Fetch modes and styles tell PHP how to get data from a database query result. They help you choose the format of the data you want to work with.
Fetch modes and styles in PHP
$stmt->fetch(PDO::FETCH_MODE);
PDO::FETCH_MODE is a constant that defines how data is returned.
You can use different fetch modes like FETCH_ASSOC, FETCH_OBJ, FETCH_NUM, etc.
$stmt->fetch(PDO::FETCH_ASSOC);
$stmt->fetch(PDO::FETCH_OBJ);
$stmt->fetch(PDO::FETCH_NUM);
$stmt->fetchColumn();
This program shows different fetch modes in PHP using PDO. It creates a small table, inserts two users, and fetches data in four ways: associative array, object, numeric array, and single column.
<?php // Connect to SQLite in memory $pdo = new PDO('sqlite::memory:'); // Create a table $pdo->exec("CREATE TABLE users (id INTEGER, name TEXT)"); // Insert data $pdo->exec("INSERT INTO users VALUES (1, 'Alice'), (2, 'Bob')"); // Prepare a query $stmt = $pdo->query("SELECT * FROM users"); // Fetch as associative array $rowAssoc = $stmt->fetch(PDO::FETCH_ASSOC); print_r($rowAssoc); // Fetch next row as object $rowObj = $stmt->fetch(PDO::FETCH_OBJ); print_r($rowObj); // Reset query $stmt = $pdo->query("SELECT * FROM users"); // Fetch first row as numeric array $rowNum = $stmt->fetch(PDO::FETCH_NUM); print_r($rowNum); // Reset query $stmt = $pdo->query("SELECT name FROM users"); // Fetch single column $name = $stmt->fetchColumn(); echo $name . "\n"; ?>
Using PDO::FETCH_ASSOC is common because it gives easy access to columns by name.
PDO::FETCH_OBJ lets you use $row->columnName which can be cleaner in some cases.
Always reset or re-run the query if you want to fetch rows again from the start.
Fetch modes control how database rows are returned in PHP.
Common modes are associative array, object, numeric array, and single column.
Choose the mode that fits how you want to use the data in your program.
Practice
Solution
Step 1: Understand fetch modes
PDO::FETCH_ASSOC returns rows as associative arrays with column names as keys.Step 2: Compare with other modes
PDO::FETCH_NUM returns numeric arrays, PDO::FETCH_OBJ returns objects, PDO::FETCH_COLUMN returns a single column value.Final Answer:
PDO::FETCH_ASSOC -> Option CQuick Check:
Associative array = PDO::FETCH_ASSOC [OK]
- Confusing PDO::FETCH_NUM with associative arrays
- Using PDO::FETCH_OBJ when array is needed
- Thinking PDO::FETCH_COLUMN returns full rows
Solution
Step 1: Identify fetch method for single row as object
fetch() returns one row; PDO::FETCH_OBJ returns it as an object.Step 2: Check other options
fetchAll() returns all rows, fetchColumn() returns a single column value, fetch(PDO::FETCH_ASSOC) returns an associative array.Final Answer:
$stmt->fetch(PDO::FETCH_OBJ); -> Option AQuick Check:
Single row object = fetch(PDO::FETCH_OBJ) [OK]
- Using fetchAll() when only one row is needed
- Using fetchColumn() to get full row
- Confusing FETCH_ASSOC with FETCH_OBJ
$stmt = $pdo->query('SELECT id, name FROM users LIMIT 1');
$row = $stmt->fetch(PDO::FETCH_NUM);
echo $row[0] . ', ' . $row[1];Solution
Step 1: Understand PDO::FETCH_NUM
fetch(PDO::FETCH_NUM) returns a numeric array with column values indexed 0,1,...Step 2: Analyze echo statement
$row[0] is the first column (id), $row[1] is the second column (name). Assuming first user has id=1 and name='Alice', output is '1, Alice'.Final Answer:
1, Alice -> Option BQuick Check:
Numeric array indexes = 0,1 values [OK]
- Expecting column names as keys with FETCH_NUM
- Using associative keys with numeric array
- Assuming fetchColumn returns full row
$stmt = $pdo->query('SELECT name FROM users');
$names = $stmt->fetch(PDO::FETCH_COLUMN);
print_r($names);Solution
Step 1: Understand fetch() vs fetchAll()
fetch() returns a single row or value, fetchAll() returns all rows.Step 2: Check usage of FETCH_COLUMN
FETCH_COLUMN fetches a single column value per row; to get all names, fetchAll(PDO::FETCH_COLUMN) is needed.Final Answer:
fetch() returns only one value, should use fetchAll() -> Option DQuick Check:
fetchAll(PDO::FETCH_COLUMN) gets all column values [OK]
- Using fetch() instead of fetchAll() for multiple rows
- Thinking FETCH_COLUMN fetches all rows automatically
- Misunderstanding print_r usage
Solution
Step 1: Fetch all rows as objects
fetchAll(PDO::FETCH_OBJ) returns an array of objects, each representing a row.Step 2: Filter rows where 'active' is true
array_filter with a callback checks the 'active' property on each object, keeping only those with true.Step 3: Check other options
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC); foreach ($rows as $r) { if ($r['active']) echo $r; } uses associative arrays but does not collect filtered rows properly; $rows = $stmt->fetchAll(PDO::FETCH_COLUMN); $activeRows = array_filter($rows, fn($r) => $r->active); uses FETCH_COLUMN which returns single column values, not objects; $rows = $stmt->fetch(PDO::FETCH_OBJ); while ($rows->active) { echo $rows; } misuses fetch() and while loop.Final Answer:
$rows = $stmt->fetchAll(PDO::FETCH_OBJ); $activeRows = array_filter($rows, fn($r) => $r->active); -> Option AQuick Check:
fetchAll(PDO::FETCH_OBJ) + array_filter for property check [OK]
- Using fetch() instead of fetchAll() for multiple rows
- Using FETCH_COLUMN when objects are needed
- Trying to filter before fetching all rows
Ready to prove it?
Pass the test and this pattern moves unknown
