Fetch a PDOStatement result one row at a time or as a bounded array.
The examples focus on the operation itself, including the important distinction: fetchAll materializes all matching rows and can use substantial memory.
What It Does
Use the examples below to observe the behavior on a small dataset before extending it to application code.
Before You Start
The examples use PHP 8.2+ features where shown. Database snippets require the corresponding PDO driver, demo tables, and credentials configured outside source code.
Example 1: Basic Use
Repeated fetch uses bounded memory; fetchAll accumulates rows in an array.
<?php
$pdo = new PDO(
"mysql:host=" . (getenv("DB_HOST") ?: "127.0.0.1") . ";dbname=" . (getenv("DB_NAME") ?: "demo") . ";charset=utf8mb4",
getenv("DB_USER") ?: "app",
getenv("DB_PASSWORD") ?: "",
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES => false]
);
$stmt = $pdo->query("SELECT id, email FROM users ORDER BY id LIMIT 3");
while (($row = $stmt->fetch(PDO::FETCH_ASSOC)) !== false) { echo $row["email"] . PHP_EOL; }
This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.
Example 2: Checking Behavior
Repeated fetch uses bounded memory; fetchAll accumulates rows in an array. Repeated fetch reads the filtered result one row at a time.
<?php
$pdo = new PDO(
"mysql:host=" . (getenv("DB_HOST") ?: "127.0.0.1") . ";dbname=" . (getenv("DB_NAME") ?: "demo") . ";charset=utf8mb4",
getenv("DB_USER") ?: "app",
getenv("DB_PASSWORD") ?: "",
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES => false]
);
$stmt = $pdo->query("SELECT id, email FROM users WHERE active=1 ORDER BY id LIMIT 4");
while (($row = $stmt->fetch(PDO::FETCH_ASSOC)) !== false) { echo $row["email"] . PHP_EOL; }
This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.
Example 3: Practical Example
Repeated fetch uses bounded memory; fetchAll accumulates rows in an array. Repeated fetch reads the filtered result one row at a time.
<?php
$pdo = new PDO(
"mysql:host=" . (getenv("DB_HOST") ?: "127.0.0.1") . ";dbname=" . (getenv("DB_NAME") ?: "demo") . ";charset=utf8mb4",
getenv("DB_USER") ?: "app",
getenv("DB_PASSWORD") ?: "",
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES => false]
);
$stmt = $pdo->query("SELECT id, email FROM users ORDER BY email LIMIT 2");
while (($row = $stmt->fetch(PDO::FETCH_ASSOC)) !== false) { echo $row["email"] . PHP_EOL; }
This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.
Important Behavior and Edge Cases
fetch returns false when rows end; do not mistake it for a valid empty row.
Comparison with Related Approaches
| Approach | Difference |
|---|---|
| Fetching Rows with PDO | Fetch a PDOStatement result one row at a time or as a bounded array. |
| Related API or alternative | fetchAll materializes all matching rows and can use substantial memory. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Ignoring the operation-specific limits | fetch returns false when rows end; do not mistake it for a valid empty row. |
| Confusing it with a related API | fetchAll materializes all matching rows and can use substantial memory. |
Key Takeaways
- Fetch a PDOStatement result one row at a time or as a bounded array.
- fetch returns false when rows end; do not mistake it for a valid empty row.
- fetchAll materializes all matching rows and can use substantial memory.
Use Fetching Rows with PDO when its documented behavior matches the problem. Test the relevant edge cases in your own runtime before deploying it.