PDO can hydrate query results as objects rather than associative arrays.
The examples focus on the operation itself, including the important distinction: FETCH_ASSOC returns arrays; FETCH_OBJ returns anonymous objects.
What It Does
Use the examples below to observe the behavior on a small dataset before extending it to application code.
Before You Start
The examples use PHP 8.2+ features where shown. Database snippets require the corresponding PDO driver, demo tables, and credentials configured outside source code.
Example 1: Basic Use
FETCH_OBJ creates stdClass instances. Typed mapping should be explicit when application invariants matter.
<?php
$pdo = new PDO(
"mysql:host=" . (getenv("DB_HOST") ?: "127.0.0.1") . ";dbname=" . (getenv("DB_NAME") ?: "demo") . ";charset=utf8mb4",
getenv("DB_USER") ?: "app",
getenv("DB_PASSWORD") ?: "",
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES => false]
);
$stmt = $pdo->query("SELECT id, email FROM users ORDER BY id LIMIT 3");
while ($row = $stmt->fetch(PDO::FETCH_OBJ)) { echo $row->email . PHP_EOL; }
This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.
Example 2: Checking Behavior
FETCH_OBJ creates stdClass instances. Typed mapping should be explicit when application invariants matter. FETCH_OBJ still returns objects regardless of the query ordering.
<?php
$pdo = new PDO(
"mysql:host=" . (getenv("DB_HOST") ?: "127.0.0.1") . ";dbname=" . (getenv("DB_NAME") ?: "demo") . ";charset=utf8mb4",
getenv("DB_USER") ?: "app",
getenv("DB_PASSWORD") ?: "",
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES => false]
);
$stmt = $pdo->query("SELECT id, email FROM users WHERE active=1 ORDER BY email LIMIT 4");
while ($row = $stmt->fetch(PDO::FETCH_OBJ)) { echo $row->email . PHP_EOL; }
This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.
Example 3: Practical Example
FETCH_OBJ creates stdClass instances. Typed mapping should be explicit when application invariants matter. FETCH_OBJ still returns objects regardless of the query ordering.
<?php
$pdo = new PDO(
"mysql:host=" . (getenv("DB_HOST") ?: "127.0.0.1") . ";dbname=" . (getenv("DB_NAME") ?: "demo") . ";charset=utf8mb4",
getenv("DB_USER") ?: "app",
getenv("DB_PASSWORD") ?: "",
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES => false]
);
$stmt = $pdo->query("SELECT id, email FROM users ORDER BY email LIMIT 2");
while ($row = $stmt->fetch(PDO::FETCH_OBJ)) { echo $row->email . PHP_EOL; }
This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.
Important Behavior and Edge Cases
FETCH_CLASS constructor/typing behavior requires attention; mapping deliberately is often clearer.
Comparison with Related Approaches
| Approach | Difference |
|---|---|
| Fetching Objects with PDO | PDO can hydrate query results as objects rather than associative arrays. |
| Related API or alternative | FETCH_ASSOC returns arrays; FETCH_OBJ returns anonymous objects. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Ignoring the operation-specific limits | FETCH_CLASS constructor/typing behavior requires attention; mapping deliberately is often clearer. |
| Confusing it with a related API | FETCH_ASSOC returns arrays; FETCH_OBJ returns anonymous objects. |
Key Takeaways
- PDO can hydrate query results as objects rather than associative arrays.
- FETCH_CLASS constructor/typing behavior requires attention; mapping deliberately is often clearer.
- FETCH_ASSOC returns arrays; FETCH_OBJ returns anonymous objects.
Use Fetching Objects with PDO when its documented behavior matches the problem. Test the relevant edge cases in your own runtime before deploying it.