0
0
PHPprogramming~10 mins

Fetch modes and styles in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Fetch modes and styles
Connect to DB
Execute Query
Fetch Data
Fetch as Associative Array
Fetch as Numeric Array
Fetch as Object
Fetch Both
Fetch into Class
Use Data
This flow shows how after running a query, PHP fetches data in different styles or modes, like arrays or objects, to use in your program.
Execution Sample
PHP
<?php
$stmt = $pdo->query('SELECT id, name FROM users');
$row = $stmt->fetch(PDO::FETCH_ASSOC);
echo $row['name'];
?>
This code fetches one row from the database as an associative array and prints the 'name' field.
Execution Table
StepActionFetch ModeData ReturnedOutput
1Execute query---
2Fetch first rowPDO::FETCH_ASSOC{'id' => 1, 'name' => 'Alice'}Alice
3Fetch second rowPDO::FETCH_ASSOC{'id' => 2, 'name' => 'Bob'}-
4Fetch third rowPDO::FETCH_ASSOCfalse (no more rows)-
💡 No more rows to fetch, fetch() returns false and loop ends
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4
$rownull{'id' => 1, 'name' => 'Alice'}{'id' => 2, 'name' => 'Bob'}false
Key Moments - 3 Insights
Why does fetch() return false at the end?
fetch() returns false when there are no more rows to get from the query result, as shown in step 4 of the execution_table.
What is the difference between FETCH_ASSOC and FETCH_NUM?
FETCH_ASSOC returns data as an associative array with column names as keys, while FETCH_NUM returns a numeric array with column indexes, shown by the 'Fetch Mode' column in the execution_table.
Can I fetch data as an object?
Yes, using FETCH_OBJ mode, fetch() returns each row as an object with properties matching column names.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of $row after step 3?
A{'id' => 2, 'name' => 'Bob'}
B{'id' => 1, 'name' => 'Alice'}
Cfalse
Dnull
💡 Hint
Check the 'Data Returned' column for step 3 in execution_table.
At which step does fetch() return false indicating no more rows?
AStep 2
BStep 3
CStep 4
DNever
💡 Hint
Look at the 'Data Returned' column in execution_table for the step where fetch() returns false.
If we change fetch mode to PDO::FETCH_NUM, how would the data look?
AAssociative array with column names as keys
BNumeric array with column indexes as keys
CObject with properties
DBoolean false
💡 Hint
Recall the difference between FETCH_ASSOC and FETCH_NUM explained in key_moments.
Concept Snapshot
PHP fetch modes control how query results are returned.
Common modes:
- FETCH_ASSOC: associative array (column names)
- FETCH_NUM: numeric array (indexes)
- FETCH_OBJ: object with properties
fetch() returns false when no rows left.
Full Transcript
This visual execution shows how PHP fetch modes work when getting data from a database. After running a query, fetch() gets one row at a time. Using FETCH_ASSOC mode, each row is an associative array with column names as keys. The variable $row changes each fetch call. When no rows remain, fetch() returns false, ending the loop. Different fetch modes change the data format: associative arrays, numeric arrays, or objects. Understanding these helps you use database results easily in your PHP code.