I'm trying to see the structure of the PDOStatement object returned by my query:
$sql="SELECT co.CompanyId, co.Name, co.Address, co.City, co.State, ctry.NameEnglish, co.PostalCode FROM company AS co LEFT JOIN country AS ctry ON ctry.CountryId = co.CountryId ORDER BY Name;";
$result=$conn->query($sql);
The query works, as I'm able to nest some foreach
statements and display the six rows of data by doing the following.
$firstRow=true;
echo '<table>';
foreach ($result as $rowId => $rowData) {
if ($firstRow) {
echo "<thead><tr><th>Row Number</th>";
foreach ($rowData as $fieldId => $fieldData) {
echo "<th>$fieldId</th>";
}
echo "</tr></thead>";
$firstRow=false;
}
echo "<tr><td>$rowId</td>";
foreach ($rowData as $fieldId => $fieldData) {
$fieldData=str_replace("\n","<br>",$fieldData);
echo "<td>$fieldData</td>";
}
echo "</tr>";
}
echo "</table>";
But I was curious about the internal structure of the object $result
, so I var_dump
'ed it and got this:
object(PDOStatement)#4 (1) {
["queryString"]=>
string(185) "SELECT co.CompanyId, co.Name, co.Address, co.City, co.State, ctry.NameEnglish, co.PostalCode FROM company AS co LEFT JOIN country AS ctry ON ctry.CountryId = co.CountryId ORDER BY Name;"
}
Why doesn't var_dump
show the arrays associated with the rows and fields?
The manual page for PDOStatement
shows what is going on here. PDOStatement
objects only have one property, queryString
. That's what you're seeing when you var_dump.
Everything else on PDOStatement
is a method, which are excluded from var_dump
.
PDOStatement implements Traversable
(Docs for Traversable
.) This is how you are able to see the rows when you foreach
. Internally, PDOStatement
implements methods that are called when you foreach
over the object. Those methods call fetch()
and get the row data. That's how you see the rows when you foreach
, but why you can't see them in a var_dump
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With