← Home ← Codex ← DEBT
Browse by Category
+ added · updated 7d
← Back to glossary

PDO Fetch Modes

PHP PHP 5.1+ Beginner
debt(d5/e1/b3/t3)
d5 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'specialist tool catches it' (d5). The detection_hints list phpstan as the tool, and the code_pattern targets fetch(PDO::FETCH_BOTH) or fetchAll() without a fetch mode — this is a specialist static analysis tool catch, not a default linter or compiler error.

e1 Effort Remediation debt — work required to fix once spotted

Closest to 'one-line patch or single-call swap' (e1). The quick_fix explicitly states: set PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC in the PDO constructor options array — a single configuration change.

b3 Burden Structural debt — long-term weight of choosing wrong

Closest to 'localised tax' (b3). The choice affects all database queries in the codebase (web and cli contexts), but it's a configuration-level decision localised to the PDO setup. Inconsistent modes across the codebase impose a moderate maintenance tax on query code, but the rest of the system is largely unaffected.

t3 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'minor surprise (one edge case)' (t3). The misconception is mild: developers may prefer FETCH_OBJ thinking it's better, but FETCH_ASSOC is safer. The more concrete trap is the default FETCH_BOTH wasting memory with duplicate keys — a documented gotcha but not catastrophic or architecturally contradictory.

About DEBT scoring →

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);

Added 31 Mar 2026
Views 36
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 2 pings S 2 pings S 1 ping M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 0 pings S 1 ping M 0 pings T 1 ping W 0 pings T 0 pings F 1 ping S 0 pings S 0 pings M 1 ping T 0 pings W
No pings yet today
SEMrush 1
Perplexity 5 Scrapy 4 Google 3 Unknown AI 3 Ahrefs 3 Bing 3 Claude 2 ChatGPT 1 Meta AI 1 Majestic 1 PetalBot 1 SEMrush 1
crawler 25 crawler_json 2 pre-tracking 1
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


✓ schema.org compliant