Content · Web Development · PHP

PHP PDOStatement::fetchAll() Method: Usage and Examples

Returns all remaining rows as an array according to the requested fetch mode. Large result sets can exhaust memory; fetch one row at a time when

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

Returns all remaining rows as an array according to the requested fetch mode.

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

What It Does


Returns all remaining rows as an array according to the requested fetch mode.

Syntax and Return Value


PDOStatement::fetchAll(Parameter #0 [ <optional> int $mode = PDO::FETCH_DEFAULT ], Parameter #1 [ <optional> mixed ...$args ]): mixed

The return value and exceptional cases depend on the call described here: Large result sets can exhaust memory; fetch one row at a time when streaming.

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]);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($rows);

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]);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($rows);

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]);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($rows);

Important Behavior and Edge Cases


Large result sets can exhaust memory; fetch one row at a time when streaming.

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

Comparison with Related Approaches


ApproachImportant distinction
PDOStatement::fetchAllReturns all remaining rows as an array according to the requested fetch mode.
Related API / patternfetch retrieves one row per call.

Common Beginner Mistakes


MistakeCorrect approach
Assuming the happy path covers edge casesLarge result sets can exhaust memory; fetch one row at a time when streaming.
Using a related API interchangeablyfetch retrieves one row per call.

Key Takeaways


  • Returns all remaining rows as an array according to the requested fetch mode.
  • Large result sets can exhaust memory; fetch one row at a time when streaming.
  • fetch retrieves one row per call.

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.