how to print the result of an SQL Query from a PDO Object


I am using PHP+PDO+MySQL to run some queries.

My queries are working fine, but i am not able to print or access the individual rows.

Here is the sql query :

$sql = "SELECT BUS_ID FROM noname WHERE STAND_ID=:start and BUS_ID in (SELECT BUS_ID FROM noname WHERE STAND_ID=:end)";

When i execute and fetch the results i get this :

$result = $stmt->fetchALL(PDO::FETCH_CLASS);
print_r ($result);

Output :

Array ( [0] => stdClass Object ( [BUS_ID] => 1 ) [1] => stdClass Object ( [BUS_ID] => 2 ) ) 

From what i understand with my little PHP knoweledge is that this is an array.
So i tried :

foreach ($result as $row)
    echo $row["BUS_ID"];

But i got no result..!!
Please tell me on how to traverse this array and get my field/column members.


Your $stmt->fetchALL(PDO::FETCH_CLASS); is returning a array of objects.
To access properties of objects you should use ->, so try this:

print $row->BUS_ID;

Inside your foreach.

Answered By – bemontibeller

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published