Skip to content
Tutorials Center
My learning

Topics

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.

PHP
foreach ($pdo->query('SELECT id, title FROM posts') as $row) {
    echo $row['title'], "\n";
}

Try it

PHP query() with foreach and fetch modes

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.

PHP Failures and the danger of concatenation

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

PHP
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, use prepare() and execute(), 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 for SELECT. It reports affected rows for INSERT/UPDATE/DELETE; use COUNT(*) 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() returns false when nothing is left, so test with === false before reading columns.
  • The default fetch mode comes from PDO::ATTR_DEFAULT_FETCH_MODE; without it, PDO::FETCH_BOTH returns every column twice (by name and by number).