PHP Reference ยท Databases (PDO)
PHP PDO::query() method
Reference for PHP PDO::query(): run an SQL statement and get a PDOStatement, loop over rows with foreach, choose a fetch mode and when to use prepare() instead.
PDO::query() sends one SQL statement to the database and returns a PDOStatement you can read rows from. It is the quick way to run a query that has no user-supplied values, such as SELECT * FROM countries ORDER BY name.
The returned statement is traversable, so you can use it directly in foreach, or call fetch(), fetchAll() or fetchColumn(). As soon as a value comes from a user, a form or a URL, use prepare() with placeholders instead, never string concatenation.
foreach ($pdo->query('SELECT id, title FROM posts') as $row) { echo $row['title'], "\n"; }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
PDO::query(string $query, ?int $fetchMode = null): PDOStatement|false PDO::query(string $query, int $fetchMode = PDO::FETCH_COLUMN, int $colno): PDOStatement|false PDO::query(string $query, int $fetchMode = PDO::FETCH_CLASS, string $class, array $constructorArgs): PDOStatement|false
| Name | Description |
|---|---|
$query |
The SQL statement. It must not contain unescaped user data. |
$fetchMode |
Optional default fetch mode for this statement, such as PDO::FETCH_ASSOC, PDO::FETCH_OBJ, PDO::FETCH_NUM, PDO::FETCH_COLUMN, PDO::FETCH_KEY_PAIR. |
$colno |
With FETCH_COLUMN: which column to return, counting from 0. |
$class, $constructorArgs |
With FETCH_CLASS: the class to build and its constructor arguments. |
Return value: a PDOStatement, or false on failure when the error mode is not exception mode.
Errors / Exceptions: with PDO::ERRMODE_EXCEPTION (the default since PHP 8.0), a failed query throws PDOException (invalid SQL, missing table, constraint violation). With ERRMODE_WARNING it raises a warning and returns false; with ERRMODE_SILENT it returns false and you check errorInfo().
Usage notes
- No parameters.
query()cannot bind values. For anything with input, useprepare()andexecute(), as the second example shows. - One statement. Send a single statement; some drivers allow several, but relying on that makes injection worse.
- Unbuffered results and MySQL. With the MySQL driver, finish reading (or call
closeCursor()on) one result before running the next query on the same connection, or you may get "Cannot execute queries while other unbuffered queries are active". rowCount()is not reliable forSELECT. It reports affected rows forINSERT/UPDATE/DELETE; useCOUNT(*)or count the fetched rows.- For statements that return no rows, use
PDO::exec(), which returns the number of affected rows directly. - A missing row is
false.fetch()returnsfalsewhen nothing is left, so test with=== falsebefore reading columns. - The default fetch mode comes from
PDO::ATTR_DEFAULT_FETCH_MODE; without it,PDO::FETCH_BOTHreturns every column twice (by name and by number).