0
0
PHPprogramming~10 mins

Fetching results (fetch, fetchAll) in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Fetching results (fetch, fetchAll)
Execute Query
Fetch One Row?
NoExit
Yes
Return Row Data
Fetch Next Row or fetchAll
Back to Fetch One Row or Return All Rows
This flow shows how PHP fetches data from a database query, either one row at a time or all rows at once.
Execution Sample
PHP
<?php
$stmt = $pdo->query('SELECT id, name FROM users');
$row = $stmt->fetch();
$allRows = $stmt->fetchAll();
?>
This code runs a query, fetches one row, then fetches all remaining rows.
Execution Table
StepActionMethod CalledResultNotes
1Run query 'SELECT id, name FROM users'query()PDOStatement objectQuery executed, ready to fetch
2Fetch first rowfetch()Array with first row dataReturns one row as associative array
3Fetch all remaining rowsfetchAll()Array of arrays with remaining rowsReturns all rows left in result set
4Try fetch againfetch()falseNo more rows, fetch returns false
5Try fetchAll againfetchAll()Empty arrayNo rows left, returns empty array
💡 No more rows to fetch, fetch returns false and fetchAll returns empty array
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5
$stmtnullPDOStatement objectPDOStatement objectPDOStatement objectPDOStatement object
$rownullFirst row arrayFirst row arrayfalsefalse
$allRowsnullnullArray of remaining rowsArray of remaining rowsEmpty array
Key Moments - 3 Insights
Why does fetch() return false after fetching all rows?
Because fetch() moves forward one row at a time and returns false when no rows remain, as shown in step 4 of the execution_table.
What does fetchAll() return if called after all rows are fetched?
It returns an empty array since there are no rows left to fetch, as shown in step 5 of the execution_table.
Does fetchAll() include the row already fetched by fetch()?
No, fetchAll() returns only the rows remaining after fetch() has moved the pointer, so the first row is not included, as seen between steps 2 and 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of $row after step 2?
AAn empty array
Bfalse
CAn array with the first row data
DPDOStatement object
💡 Hint
Check the 'Result' column in step 2 of execution_table.
At which step does fetch() return false?
AStep 3
BStep 4
CStep 5
DStep 2
💡 Hint
Look at the 'Result' column for fetch() in execution_table steps.
If fetch() was not called before fetchAll(), what would fetchAll() return at step 3?
AAll rows from the query
BOnly the first row
CAn empty array
Dfalse
💡 Hint
Recall that fetchAll() returns all rows left; if none fetched yet, it returns all rows.
Concept Snapshot
PHP fetch() gets one row from query result as an array.
fetchAll() gets all remaining rows as an array of arrays.
fetch() returns false when no rows left.
fetchAll() returns empty array if no rows left.
Use fetch() to process rows one by one.
Use fetchAll() to get all rows at once.
Full Transcript
This visual trace shows how PHP fetches data from a database query using fetch() and fetchAll(). First, the query runs and returns a PDOStatement object. Then fetch() gets the first row as an array. Next, fetchAll() gets all remaining rows as an array of arrays. If fetch() is called again after all rows are fetched, it returns false. If fetchAll() is called again, it returns an empty array. This helps understand how to retrieve data step-by-step or all at once from a database in PHP.