Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

var_dump of PDOStatement Object is not showing returned data

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?

like image 900
Jonathan M Avatar asked Jan 19 '15 17:01

Jonathan M


1 Answers

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.

like image 90
Rocket Hazmat Avatar answered Oct 04 '22 07:10

Rocket Hazmat