Runs a SQL statement immediately and returns a PDOStatement for result-producing statements.
The objective is to understand the behavior and operational limits, not only the method name.
What It Does
Runs a SQL statement immediately and returns a PDOStatement for result-producing statements.
Syntax and Return Value
PDO::query(Parameter #0 [ <required> string $query ], Parameter #1 [ <optional> ?int $fetchMode = null ], Parameter #2 [ <optional> mixed ...$fetchModeArgs ]): mixed
The return value and exceptional cases depend on the call described here: Never concatenate user input into a query string; use prepare for dynamic values.
Example 1: Basic Use
This self-contained SQLite example uses in-memory demo data; it requires the pdo_sqlite driver.
<?php
$pdo = new PDO('sqlite::memory:', null, null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$pdo->exec('CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT NOT NULL, price INTEGER NOT NULL, active INTEGER)');
$pdo->exec("INSERT INTO products (id,name,price,active) VALUES (1,'Desk',120,1),(2,'Lamp',35,1),(3,'Chair',85,0)");
$result = $pdo->query("SELECT name FROM products WHERE price >= 48 ORDER BY id");
foreach ($result as $row) { echo $row["name"] . PHP_EOL; }
Example 2: A Second Scenario
This self-contained SQLite example uses in-memory demo data; it requires the pdo_sqlite driver.
<?php
$pdo = new PDO('sqlite::memory:', null, null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$pdo->exec('CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT NOT NULL, price INTEGER NOT NULL, active INTEGER)');
$pdo->exec("INSERT INTO products (id,name,price,active) VALUES (1,'Desk',120,1),(2,'Lamp',35,1),(3,'Chair',85,0)");
$result = $pdo->query("SELECT name FROM products WHERE price >= 62 ORDER BY id");
foreach ($result as $row) { echo $row["name"] . PHP_EOL; }
Example 3: Practical Checks
This self-contained SQLite example uses in-memory demo data; it requires the pdo_sqlite driver.
<?php
$pdo = new PDO('sqlite::memory:', null, null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$pdo->exec('CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT NOT NULL, price INTEGER NOT NULL, active INTEGER)');
$pdo->exec("INSERT INTO products (id,name,price,active) VALUES (1,'Desk',120,1),(2,'Lamp',35,1),(3,'Chair',85,0)");
$result = $pdo->query("SELECT name FROM products WHERE price >= 79 ORDER BY id");
foreach ($result as $row) { echo $row["name"] . PHP_EOL; }
Important Behavior and Edge Cases
Never concatenate user input into a query string; use prepare for dynamic values.
For production use, also account for error handling, data validation, resource limits and environment-specific driver behavior.
Comparison with Related Approaches
| Approach | Important distinction |
|---|---|
| PDO::query | Runs a SQL statement immediately and returns a PDOStatement for result-producing statements. |
| Related API / pattern | exec is intended for statements without result rows. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Assuming the happy path covers edge cases | Never concatenate user input into a query string; use prepare for dynamic values. |
| Using a related API interchangeably | exec is intended for statements without result rows. |
Key Takeaways
- Runs a SQL statement immediately and returns a PDOStatement for result-producing statements.
- Never concatenate user input into a query string; use prepare for dynamic values.
- exec is intended for statements without result rows.
Choose this API or pattern when its exact semantics match your task, and verify the result against the database driver and runtime used by your application.