PDO::query — Execute SQL statements and return PDOStatement objects, which can be understood as result sets (PHP 5 >= 5.1.0, PECL pdo >= 0.2.0)
public PDOStatement PDO::query ( string $statement )
public PDOStatement PDO::query ( string $statement , int $PDO::FETCH_COLUMN , int $colno )
public PDOStatement PDO::query ( string $statement , int $PDO::FETCH_CLASS , string $classname , array $ctorargs )
public PDOStatement PDO::query ( string $statement , int $PDO::FETCH_INTO , object $object )
PDO::query() calls and executes the SQL statement in a separate function, returns the result set (if any), and the statement is returned as a PDOStatement object.
statementThe SQL statement to be executed.
PDO::query() returns a PDOStatement object if successful, and FALSE if failed.
Iterate over the output result set:
<?phpfunction getFruit($conn) { $sql = 'SELECT name, color, calories FROM fruit ORDER BY name'; foreach ($conn->query($sql) as $row) { print $row['name'] . "t"; print $row['color'] . "t"; print $row['calories'] . "n"; }}?>
The above output is:
apple red 150banana yellow 250kiwi brown 75lemon yellow 25orange orange 300pear green 150watermelon pink 90