What does PDO::query actually do ?
Does it query the SQL ?
If it queries the SQL, I think the data must be in the PDOStatement object as the result of the query..
But, what I find is just the SQL statement itself. So, I think it does nothing but just store the sql statement to the PDO statement property.

public 'queryString' => string 'SELECT * from posts' (length=19)

And, to get the data, we must fetch it using PDOStatement::fetch
So, what does PDO::query do ?
Why doesn't it just get the data ?
Is there any principle of design that makes PDO like this ?


you actually get the result, just loop it:

$result = $pdo->query("SELECT * FROM posts");

foreach ($result as $key => $value)
    echo $value['title'];

echo 'Total: '. $result->rowCount();

It's not visible through print_r() because the statement is an iterable object, if you want the full array use fetchAll():


As second parameter you can define the fetch mode, for example:

$result = $pdo->query("SELECT * FROM posts", PDO::FETCH_ASSOC);

For more information check the PDOStatement class:

Also, read the following article, at the end there is a paragraph dedicated to PDO::FETCH_CLASS method, which explains a bit how flexible can be a PDO statement:

commented: nice detailed explainations +4
commented: Thanks for your nice answer +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.