Good day.

There is the following table with tbldata data in it there are 5 fields:
id , hotel_name , city_code , city_name , url .
There is the following code:

 $stmt = $pdo->prepare("SELECT * FROM tbldata"); $stmt->execute(); $result = $stmt->fetchAll(PDO::FETCH_CLASS); var_dump($result); 

But nothing is displayed, an empty result. What is wrong, works every other day (figuratively), it works, it happens not.
Help me please.

    2 answers 2

    First of all, it is necessary to include in the PDO the mode of generating exceptions, for which a connection should specify the corresponding parameter:

     $dsn = "mysql:host=$host;dbname=$db;charset=$charset"; $opt = array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, ); $pdo = new PDO($dsn, $user, $pass, $opt); 

    And secondly, make sure that PHP errors are visible to the developer. if an error occurs in PHP, then he will always report it. The task of the PHP user is to always see all messages about errors that occur in his scripts. Thus, he will be informed about the causes of a particular problem, without the need to contact other sites.

    Well, make sure that the database has data that meets the query.

    When sampling large amounts of data, you should avoid using the fetchall function, and you should loop through the rows one at a time through fetch.

    On the pages shown to users of the site, you should avoid samples of large volumes of data in principle.

    • I turned on the exception mode, launched a new one, it’s still empty, the data is in the table, the same query in phpmyadmin results. - El Salvadore
    • I found an error, too much data is in the sample, so the result was not displayed, I set LIMIT 100 data was displayed, is there any way to get around this restriction in the table with over 600 000 entries. - El Salvadore
    • 2
      @ElSalvadore I am sure that there is a limitation. And the output of all 600K records is an anti-pattern. Use shift: LIMIT 5,10 - displays c 6 to 15 record. - romeo

    First, why make $pdo->prepare if the request contains no parameters. Secondly, when $stmt->fetchAll(PDO::FETCH_CLASS) you need to specify the class name, if not mistaken.

    • Optionally, it works in other scripts, only it does not work, probably because there is a lot of data, I tried to do through the while and PDO::fetch() , but it processes only one record, and it completes. - El Salvadore
    • Well, yes, the fetchAll function fetchAll not be used when the query returns a lot of data. But about while and PDO::fetch() , what does not work? Sample code did not lead. - Vasil Baymurzin
    • Thank you Vasil, did this: $query = "SELECT * FROM tbldata"; foreach ($pdo->query($query, PDO::FETCH_OBJ) as $key) { works on LAN, and now problems with the server, local computer I have ubuntu, and a server on the windows server and there is a WAMP warning that crashes There are no libraries, although they are all there and connected, maybe because of this. - El Salvadore
    • Specifically, what does not work for WAMP? If the problem is in the PDO module, then I recommend reading the instructions for installing and configuring this module in Windows link . - Vasil Baymurzin
    • Warning: PHP Startup: Unable to load dynamic library C:\Server\PHP\ext\php_interbase.dll (php_oci8_12.dll, php_pdo_firebird.dll, php_pdo_oci.dll, php_pspell.dll, php_sysbase_ct.dll) not be found. in Unknown on line 0 Warning: PHP Startup: Unable to load dynamic library C:\Server\PHP\ext\php_interbase.dll (php_oci8_12.dll, php_pdo_firebird.dll, php_pdo_oci.dll, php_pspell.dll, php_sysbase_ct.dll) not be found. in Unknown on line 0 PHP 5.6 Version PHP 5.6 - El Salvadore