Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>you have to fetch the results in a array, and then echo the elements of the array.</p> <pre><code>$db = new PDO("mysql:host=$db_hostname;dbname=$database", $db_username, $db_password); $sql = "SELECT exp FROM login ORDER BY number DESC LIMIT 10"; if ($stmt = $db-&gt;query($sql)) //PDO::query() returns a PDOStatement on success or false on failure. { //If we got a PDOStatement as a return value from PDO::Query() fetch the results and echo. if($numbers = $stmt-&gt;fetchAll(PDO::FETCH_ASSOC)) //This will fetch all results in associative array. { //If the array contained data, then echo them. foreach ($numbers as $num) { echo $num['exp'] . "&lt;br /&gt;"; } } else { //If the PDOStatement returned an empty array. Let us know. echo "No data in the array"; } } else { //If PDO::Query returned false, then something is wrong with our query. Or connection or whatever. echo "Query failed."; } </code></pre> <p>In queries that return large results I wouldn't use $stmt->fetchAll(). I would use fetch in a while loop like this:</p> <pre><code>$db = new PDO("mysql:host=$db_hostname;dbname=$database", $db_username, $db_password); $sql = "SELECT exp FROM login ORDER BY number DESC LIMIT 10"; if ($stmt = $db-&gt;query($sql)) //PDO::query() returns a PDOStatement on success or false on failure. { //If we got a PDOStatement as a return value from PDO::Query() !!!ECHO WHILE FETCHING!!! while($row = $stmt-&gt;fetch(PDO::FETCH_ASSOC)) //This loop will keep going for as many rows as the PDOStatement returns. { echo $row['exp'] . "&lt;br /&gt;"; } } else { //If PDO::Query returned false, then something is wrong with our query. Or connection or whatever. echo "Query failed."; } </code></pre> <p>The difference between the first code chunk and the second is that in 1st chunk, we fetch all the results in a array and print them. In the second one tho, we print the data as we retrieve them one by one with PDOStatement::fetch()</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload