$dbh = new PDO ("sqlite:/Library/WebServer/Documents/nwind2009.db3");
$sql = "SELECT * FROM Customers";
print_r($dbh->query($sql));
This returns:
PDOStatement Object ( [queryString] => SELECT * FROM Customers )
but if I do:
foreach ($dbh->query($sql) as $row) {
echo $row['CompanyName'];
}
I get the data.
Why doesn’t print_r show the database results? What special thing is happening in the foreach? I thought print_r showed me the array and that that was what I was doing in my foreach.
Thanks.
If you iterate over the query, it will automatically fetch the data. This is a good thing, because the adapter will lazy load the data the moment it is actually needed.
If you want to force the PDO adapter to fetch the data in advance, you can run the
fetchAllmethod like so: