Using PDO::query to Execute SQL and Return a PDOStatement in PHP
This article explains how the PDO::query method in PHP executes an SQL statement, returns the result as a PDOStatement object, details its parameters and return values, and provides a practical code example for retrieving and printing query results.
PDO::query is a method in PHP's PDO class that executes an SQL statement and returns the result set as a PDOStatement object.
Signature: public PDOStatement PDO::query(string $statement, int $fetch_mode = PDO::FETCH_CLASS, string $classname, array $ctorargs)
Parameters:
$statement – The SQL query to prepare and execute.
Return value: Returns a PDOStatement object on success or FALSE on failure.
Example:
query($sql) as $row) {
print $row['name'] . "\t";
print $row['color'] . "\t";
print $row['calories'] . "\n";
}
}
?>Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.