Returns the next row, or false when no more rows remain.
The objective is to understand the behavior and operational limits, not only the method name.
What It Does
Returns the next row, or false when no more rows remain.
Syntax and Return Value
PDOStatement::fetch(Parameter #0 [ <optional> int $mode = PDO::FETCH_DEFAULT ], Parameter #1 [ <optional> int $cursorOrientation = PDO::FETCH_ORI_NEXT ], Parameter #2 [ <optional> int $cursorOffset = 0 ]): mixed
The return value and exceptional cases depend on the call described here: Choose a fetch mode to avoid mixed numeric and associative keys.
Example 1: Basic Use
The statement has a real query and result set; database-specific metadata and statement options may be unsupported.
<?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)");
$stmt = $pdo->prepare("SELECT id, name, price FROM products WHERE id >= :min ORDER BY id");
$stmt->execute(["min" => 1]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
echo json_encode($row);
Example 2: A Second Scenario
The statement has a real query and result set; database-specific metadata and statement options may be unsupported.
<?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)");
$stmt = $pdo->prepare("SELECT id, name, price FROM products WHERE id >= :min ORDER BY id");
$stmt->execute(["min" => 2]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
echo json_encode($row);
Example 3: Practical Checks
The statement has a real query and result set; database-specific metadata and statement options may be unsupported.
<?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)");
$stmt = $pdo->prepare("SELECT id, name, price FROM products WHERE id >= :min ORDER BY id");
$stmt->execute(["min" => 3]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
echo json_encode($row);
Important Behavior and Edge Cases
Choose a fetch mode to avoid mixed numeric and associative keys.
For production use, also account for error handling, data validation, resource limits and environment-specific driver behavior.
Comparison with Related Approaches
| Approach | Important distinction |
|---|---|
| PDOStatement::fetch | Returns the next row, or false when no more rows remain. |
| Related API / pattern | fetchAll loads the entire remaining result at once. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Assuming the happy path covers edge cases | Choose a fetch mode to avoid mixed numeric and associative keys. |
| Using a related API interchangeably | fetchAll loads the entire remaining result at once. |
Key Takeaways
- Returns the next row, or false when no more rows remain.
- Choose a fetch mode to avoid mixed numeric and associative keys.
- fetchAll loads the entire remaining result at once.
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.