Configures the default way future fetch calls shape rows.
The objective is to understand the behavior and operational limits, not only the method name.
What It Does
Configures the default way future fetch calls shape rows.
Syntax and Return Value
PDOStatement::setFetchMode(Parameter #0 [ <required> int $mode ], Parameter #1 [ <optional> mixed ...$args ]): mixed
The return value and exceptional cases depend on the call described here: It does not fetch any rows by itself; use fetch or fetchAll afterward.
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]);
$stmt->setFetchMode(PDO::FETCH_ASSOC);
echo json_encode($stmt->fetch());
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]);
$stmt->setFetchMode(PDO::FETCH_ASSOC);
echo json_encode($stmt->fetch());
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]);
$stmt->setFetchMode(PDO::FETCH_ASSOC);
echo json_encode($stmt->fetch());
Important Behavior and Edge Cases
It does not fetch any rows by itself; use fetch or fetchAll afterward.
For production use, also account for error handling, data validation, resource limits and environment-specific driver behavior.
Comparison with Related Approaches
| Approach | Important distinction |
|---|---|
| PDOStatement::setFetchMode | Configures the default way future fetch calls shape rows. |
| Related API / pattern | fetch(PDO::FETCH_ASSOC) chooses a mode for a single call. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Assuming the happy path covers edge cases | It does not fetch any rows by itself; use fetch or fetchAll afterward. |
| Using a related API interchangeably | fetch(PDO::FETCH_ASSOC) chooses a mode for a single call. |
Key Takeaways
- Configures the default way future fetch calls shape rows.
- It does not fetch any rows by itself; use fetch or fetchAll afterward.
- fetch(PDO::FETCH_ASSOC) chooses a mode for a single 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.