Rohith R
Rohith R

Reputation: 1329

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.

Upvotes: 2

Views: 2382

Answers (1)

b14r
b14r

Reputation: 362

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.

Upvotes: 3

Related Questions