PDO Fetch Modes
Also Known As
PDO::FETCH_ASSOC
PDO::FETCH_OBJ
PDO::FETCH_CLASS
fetchAll modes
TL;DR
Constants controlling how PDO returns rows — as arrays, objects, or custom classes.
Explanation
PDO::FETCH_ASSOC returns associative arrays (column name keys). PDO::FETCH_OBJ returns stdClass objects. PDO::FETCH_CLASS maps rows directly into a specified class. PDO::FETCH_COLUMN returns a single column as a flat array. PDO::FETCH_KEY_PAIR returns a two-column result as key→value pairs. Setting PDO::ATTR_DEFAULT_FETCH_MODE on the connection avoids repeating the constant on every fetch() call.
Common Misconception
✗ FETCH_OBJ is better than FETCH_ASSOC. Both are fine — FETCH_ASSOC is safer because array access is explicit and less likely to collide with object method names.
Why It Matters
Choosing the right fetch mode reduces boilerplate — FETCH_ASSOC is safe and predictable, FETCH_CLASS removes manual mapping from result arrays to domain objects.
Common Mistakes
- Using the default FETCH_BOTH which returns both numeric and named keys, wasting memory.
- Forgetting to set a default fetch mode and using different modes inconsistently across the codebase.
- Using FETCH_OBJ on untrusted data where property names may conflict with magic methods.
Avoid When
- Avoid FETCH_BOTH — it duplicates every value under both numeric and named keys, wasting memory.
- Avoid FETCH_OBJ on untrusted column names — property names from user-controlled data can shadow object methods.
When To Use
- Use FETCH_ASSOC as the default for all queries — explicit, predictable, no memory waste.
- Use FETCH_CLASS when mapping query results directly to domain model objects.
- Use FETCH_KEY_PAIR when building id→name lookup maps from two-column queries.
Code Examples
✗ Vulnerable
// FETCH_BOTH wastes memory — every value stored twice
$row = $stmt->fetch(PDO::FETCH_BOTH); // ['id'=>1, 0=>1, 'email'=>'...', 1=>'...']
✓ Fixed
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
// FETCH_ASSOC — safe default
$row = $stmt->fetch(); // ['id' => 1, 'email' => '...']
// FETCH_CLASS — auto-map to domain object
$stmt->setFetchMode(PDO::FETCH_CLASS, User::class);
$user = $stmt->fetch(); // User instance with properties set
// FETCH_COLUMN — flat array of one column
$emails = $stmt->fetchAll(PDO::FETCH_COLUMN, 0);
// FETCH_KEY_PAIR — id => name map
$map = $pdo->query('SELECT id, name FROM roles')->fetchAll(PDO::FETCH_KEY_PAIR);
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
31 Mar 2026
Views
14
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
No pings yesterday
Perplexity 5
Unknown AI 3
Google 2
ChatGPT 1
Ahrefs 1
Also referenced
How they use it
crawler 11
pre-tracking 1
Related categories
⚡
DEV INTEL
Tools & Severity
⚙ Fix effort: Low
⚡ Quick Fix
Set PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC in the PDO constructor options array
📦 Applies To
PHP 5.1+
web
cli
🔗 Prerequisites
🔍 Detection Hints
fetch(PDO::FETCH_BOTH) or fetchAll() without fetch mode set
Auto-detectable:
✓ Yes
phpstan
🤖 AI Agent
Confidence: High
False Positives: Low
✓ Auto-fixable
Fix: Low
Context: Line