PDO fetch modes
A PHP query hands back rows in the shape you ask for, and the shape is a fetch mode of PDO. The examples below read a small SQLite table with three modes, so every SQL statement and every line of output can be checked on this page.
FETCH_ASSOC
Assoc mode returns each row as an array keyed by column name. It is the clearest default for templates and JSON output.
Fetch all rows at once for small results, and loop row by row for larger sets.
<?php $pdo = new PDO("sqlite::memory:"); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $pdo->exec("CREATE TABLE ledger (id INTEGER PRIMARY KEY, amount INTEGER)"); $pdo->exec("INSERT INTO ledger (amount) VALUES (100), (250)"); $stmt = $pdo->query("SELECT id, amount FROM ledger"); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); foreach ($rows as $row) { echo $row["id"], " ", $row["amount"], PHP_EOL; }
php assoc.php
1 100
2 250
Each line is a row, and the keys are the column names, so no numeric index can point at the wrong field.
FETCH_CLASS
Class mode builds an object of the given type for every row. Column values fill matching public properties by name.
This form keeps business code tidy, because rows arrive as typed models instead of plain arrays.
<?php class LedgerRow { public int $id = 0; public int $amount = 0; } $pdo = new PDO("sqlite::memory:"); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $pdo->exec("CREATE TABLE ledger (id INTEGER PRIMARY KEY, amount INTEGER)"); $pdo->exec("INSERT INTO ledger (amount) VALUES (100), (250)"); $stmt = $pdo->query("SELECT id, amount FROM ledger"); $rows = $stmt->fetchAll(PDO::FETCH_CLASS, LedgerRow::class); echo get_class($rows[0]), PHP_EOL; echo $rows[1]->amount, PHP_EOL; var_dump($rows[0]->id);
php objects.php
LedgerRow
250
int(1)
The property types promise integers and the dump proves it: the value arrives as int(1), not as a string.
FETCH_COLUMN
Column mode returns a flat list of values from one column. It fits id lists, names, and totals without extra wrapping.
Pass the column index starting from zero to pick which field becomes the plain value.
<?php $pdo = new PDO("sqlite::memory:"); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $pdo->exec("CREATE TABLE ledger (id INTEGER PRIMARY KEY, amount INTEGER)"); $pdo->exec("INSERT INTO ledger (amount) VALUES (100), (250)"); $stmt = $pdo->query("SELECT id, amount FROM ledger"); $amounts = $stmt->fetchAll(PDO::FETCH_COLUMN, 1); echo implode(", ", $amounts), PHP_EOL;
php column.php
100, 250
Index one picks the second column of the select, so the list holds the amounts and not the ids.
Lazy cursors note
Large results should stream through a cursor instead of loading all rows at once. Fetch inside a loop and close the cursor when done.
This keeps memory flat while processing thousands of rows in background jobs.
<?php $pdo = new PDO("sqlite::memory:"); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $pdo->exec("CREATE TABLE ledger (id INTEGER PRIMARY KEY, amount INTEGER)"); $pdo->exec("INSERT INTO ledger (amount) VALUES (100), (250)"); $stmt = $pdo->query("SELECT id, amount FROM ledger"); while (($row = $stmt->fetch(PDO::FETCH_ASSOC)) !== false) { echo $row["id"], PHP_EOL; } $stmt->closeCursor(); echo "done", PHP_EOL;
php cursor.php
1
2
done
The loop asks for one row at a time, and the cursor is released before the next statement runs.
Exercises
- Add a third row and count the rows each mode returns.
- Fetch the same query twice from one statement and explain the empty second result.
- Add a method to LedgerRow that formats the amount and call it on a fetched object.
- Make one property private and see what class mode does with the value.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Which fetch mode keys every row by column name?
Answer
PDO::FETCH_ASSOCFETCH_ASSOCbuilds an array per row and uses the column names as its keys. -
What does
PDO::FETCH_CLASSdo with each row?Answer
It returns one object of the given class per row
Every row becomes an instance of the class you name, and matching public properties take the values.
-
What does
PDO::FETCH_COLUMNreturn?Answer
A flat list of values from one column
It keeps one field per row, which is why it fits id lists and totals.
-
Why does a loop with
fetch()keep memory flat on a big table?Answer
It takes one row per call instead of all of them
Only the current row is in memory, so the size of the result does not matter.
-
What does this program print?
<?php $pdo = new PDO("sqlite::memory:"); $pdo->exec("CREATE TABLE ledger (id INTEGER PRIMARY KEY, amount INTEGER)"); $pdo->exec("INSERT INTO ledger (amount) VALUES (100), (250)"); $rows = $pdo->query("SELECT id, amount FROM ledger")->fetchAll(PDO::FETCH_ASSOC); echo count($rows), " ", $rows[1]["amount"];Answer
2 250
Two rows arrive, and index one is the row whose amount is 250.
-
What does this program print?
<?php $pdo = new PDO("sqlite::memory:"); $pdo->exec("CREATE TABLE ledger (id INTEGER PRIMARY KEY, amount INTEGER)"); $pdo->exec("INSERT INTO ledger (amount) VALUES (100), (250)"); $stmt = $pdo->query("SELECT id, amount FROM ledger"); $first = $stmt->fetchAll(PDO::FETCH_ASSOC); $second = $stmt->fetchAll(PDO::FETCH_ASSOC); echo count($first), " ", count($second);Answer
2 0
The first call reads every row, so the cursor is at the end and the second call finds nothing.
-
What does this program print?
<?php $pdo = new PDO("sqlite::memory:"); $pdo->exec("CREATE TABLE ledger (id INTEGER PRIMARY KEY, amount INTEGER)"); $pdo->exec("INSERT INTO ledger (amount) VALUES (100), (250)"); $amounts = $pdo->query("SELECT id, amount FROM ledger")->fetchAll(PDO::FETCH_COLUMN, 1); echo implode(", ", $amounts);Answer
100, 250
The index counts from zero inside the select, so one is the amount column and the order stays the table order.
-
What does this program print?
<?php class Row { public int $amount = 0; } $pdo = new PDO("sqlite::memory:"); $pdo->exec("CREATE TABLE ledger (amount INTEGER)"); $pdo->exec("INSERT INTO ledger (amount) VALUES (7)"); $rows = $pdo->query("SELECT amount FROM ledger")->fetchAll(PDO::FETCH_CLASS, Row::class); var_dump($rows[0]->amount);Answer
int(7)
The declared type is int, so the value from the row fills the property as an integer.
-
A fresh statement is fetched with
fetch()once and then fetched again. What does the second call return on a one-row result?Answer
falseThe cursor is past the last row, and
fetch()reports that withfalse. -
Why must a query that comes back from
query()be fetched before another query uses the same connection?Answer
The unfetched result can block the next statement
On many drivers an open result holds the connection until it is read or its cursor is closed, so fetch and free it first.
The programs on this page were run with PHP 8.5.13 (php -v) on SQLite in memory, with the commands shown above.
Article author: Arthur Isaev