Content · Web Development · PHP

Reading Multiple Rows with PDO

Iterate over database rows with fetch in a loop to control memory use. fetchAll is convenient for small result sets but loads the complete result into PHP

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

Iterate over database rows with fetch in a loop to control memory use.

The objective is to understand the behavior and operational limits, not only the method name.

What It Does


Iterate over database rows with fetch in a loop to control memory use.

Prerequisites


Use a dedicated development environment and sample data. Where a snippet uses an injected connection, repository, request value or mailer, supply it from your own application setup.

Example 1: Basic Use


A focused working pattern for this topic. Names such as $pdo, $authContext and $repo represent dependencies provided by the application unless initialized in the snippet.

<?php
$stmt = $pdo->prepare('SELECT id, name FROM products WHERE active = :active ORDER BY id');
$stmt->execute(['active' => 1]);
while (($row = $stmt->fetch(PDO::FETCH_ASSOC)) !== false) {
    echo htmlspecialchars($row['name'], ENT_QUOTES, 'UTF-8') . PHP_EOL;
}

Example 2: A Second Scenario


A second use case that adds a different operation or data flow specific to this topic.

<?php
$stmt = $pdo->prepare('SELECT email FROM subscribers WHERE active = ? ORDER BY id');
$stmt->execute([1]);
while (($email = $stmt->fetchColumn()) !== false) echo htmlspecialchars($email, ENT_QUOTES, 'UTF-8') . PHP_EOL;

Example 3: Practical Checks


A practical validation, failure or deployment concern specific to this topic.

<?php
$stmt = $pdo->query('SELECT id, total FROM orders ORDER BY id LIMIT 100');
$total = 0;
while (($row = $stmt->fetch(PDO::FETCH_ASSOC)) !== false) $total += (float) $row['total'];
echo $total;

Important Behavior and Edge Cases


fetchAll is convenient for small result sets but loads the complete result into PHP memory.

For production use, also account for error handling, data validation, resource limits and environment-specific driver behavior.

Comparison with Related Approaches


ApproachImportant distinction
Reading Multiple Rows with PDOIterate over database rows with fetch in a loop to control memory use.
Related API / patternUse this workflow as a focused starting point rather than a substitute for application-level tests.

Common Beginner Mistakes


MistakeCorrect approach
Assuming the happy path covers edge casesfetchAll is convenient for small result sets but loads the complete result into PHP memory.
Using a related API interchangeablyUse this workflow as a focused starting point rather than a substitute for application-level tests.

Key Takeaways


  • Iterate over database rows with fetch in a loop to control memory use.
  • fetchAll is convenient for small result sets but loads the complete result into PHP memory.
  • Use this workflow as a focused starting point rather than a substitute for application-level tests.

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.

Share this resourceSend this page to someone who may find it useful.