The fetch() method retrieves the next row from a result set as an array or object. It returns one row at a time or false if no more rows are available.
Click to reveal answer
beginner
How is fetchAll() different from fetch()?
fetchAll() retrieves all remaining rows from the result set at once as an array of arrays or objects, while fetch() gets one row at a time.
Click to reveal answer
intermediate
What is a common use case for fetch()?
Use fetch() when you want to process rows one by one, for example, in a loop to handle large data sets without loading everything into memory.
Click to reveal answer
beginner
What does fetchAll(PDO::FETCH_ASSOC) return?
It returns all rows as an array of associative arrays, where each array uses column names as keys.
Click to reveal answer
beginner
What happens if you call fetch() after all rows have been fetched?
It returns false, indicating there are no more rows to fetch.
Click to reveal answer
Which method fetches all rows from a PDO statement at once?
Aprepare()
Bfetch()
Cexecute()
DfetchAll()
✗ Incorrect
fetchAll() retrieves all rows at once, while fetch() retrieves one row at a time.
What does fetch() return when no more rows are available?
Afalse
Bnull
Cempty array
D0
✗ Incorrect
fetch() returns false when there are no more rows to fetch.
Which fetch style returns rows as associative arrays with column names as keys?
APDO::FETCH_NUM
BPDO::FETCH_OBJ
CPDO::FETCH_ASSOC
DPDO::FETCH_BOTH
✗ Incorrect
PDO::FETCH_ASSOC returns rows as associative arrays keyed by column names.
When is it better to use fetch() instead of fetchAll()?
AWhen you want all rows at once
BWhen processing large data sets row by row
CWhen you want to prepare a statement
DWhen you want to execute a query
✗ Incorrect
fetch() is better for large data sets to avoid loading all rows into memory at once.
What type of value does fetchAll() return?
AAn array of rows
BAn integer
CA boolean
DA single row
✗ Incorrect
fetchAll() returns an array containing all rows from the result set.
Explain the difference between fetch() and fetchAll() in PHP PDO.
Think about how many rows each method returns and memory usage.
You got /4 concepts.
Describe a scenario where using fetch() is better than fetchAll().
Consider memory and performance when handling many rows.
You got /4 concepts.
Practice
(1/5)
1. What does the fetch() method do when used with a PDO statement in PHP?
easy
A. Retrieves one row from the query result
B. Retrieves all rows from the query result
C. Executes the SQL query
D. Closes the database connection
Solution
Step 1: Understand the purpose of fetch()
The fetch() method is designed to get a single row from the result set of a database query.
Step 2: Compare with other methods
Unlike fetchAll(), which retrieves all rows, fetch() only returns one row at a time.
Final Answer:
Retrieves one row from the query result -> Option A
Quick Check:
fetch() = one row [OK]
Hint: fetch() gets one row, fetchAll() gets all rows [OK]
Common Mistakes:
Confusing fetch() with fetchAll()
Thinking fetch() executes the query
Assuming fetch() closes the connection
2. Which of the following is the correct syntax to fetch all rows from a PDO statement $stmt in PHP?
easy
A. $rows = $stmt->fetchAll();
B. $rows = $stmt->fetch_all();
C. $rows = fetchAll($stmt);
D. $rows = $stmt->fetch();
Solution
Step 1: Identify the correct method name
The PDOStatement object uses fetchAll() to retrieve all rows as an array.
Step 2: Check method call syntax
The correct syntax is $stmt->fetchAll(); with camelCase and parentheses.
Final Answer:
$rows = $stmt->fetchAll(); -> Option A
Quick Check:
fetchAll() method syntax correct [OK]
Hint: Use fetchAll() with parentheses on the statement object [OK]
Common Mistakes:
Using fetch_all() instead of fetchAll()
Calling fetchAll() as a standalone function
Using fetch() when fetchAll() is needed
3. Given the following PHP code, what will be the output?
$stmt = $pdo->query('SELECT id FROM users LIMIT 2');
$row1 = $stmt->fetch();
$row2 = $stmt->fetch();
$row3 = $stmt->fetch();
var_dump([$row1, $row2, $row3]);
medium
A. [false, false, false]
B. [array with first row, array with second row, array with third row]
C. [array with first row, array with second row, false]
D. Syntax error
Solution
Step 1: Understand fetch() behavior on limited rows
The query returns 2 rows. Each fetch() call returns one row until no rows remain.
Step 2: Analyze each fetch() call
First fetch() returns first row array, second fetch() returns second row array, third fetch() returns false (no more rows).
Final Answer:
[array with first row, array with second row, false] -> Option C
Quick Check:
fetch() returns false after last row [OK]
Hint: fetch() returns false when no rows left [OK]
Common Mistakes:
Expecting fetch() to return null instead of false
Assuming fetch() returns empty array after last row
Thinking fetch() fetches all rows at once
4. What is wrong with this PHP code snippet?
$stmt = $pdo->query('SELECT name FROM users');
$rows = $stmt->fetch();
foreach ($rows as $row) {
echo $row['name'];
}
medium
A. fetch() returns all rows, so foreach is unnecessary
B. The variable $rows is not defined
C. The query syntax is invalid
D. fetch() returns a single row, so foreach on it causes error
Solution
Step 1: Check what fetch() returns
fetch() returns a single row as an array, not an array of rows.
Step 2: Understand foreach usage
foreach expects an array of rows, but here it loops over columns of one row, causing errors when accessing $row['name'].
Final Answer:
fetch() returns a single row, so foreach on it causes error -> Option D
Quick Check:
fetch() = one row, foreach needs multiple rows [OK]
Hint: Use fetchAll() when looping over multiple rows [OK]
Common Mistakes:
Using fetch() when fetchAll() is needed for loops
Assuming fetch() returns all rows
Not checking data structure before foreach
5. You want to fetch all users from the database but only keep those whose age is greater than 18. Which code snippet correctly uses fetchAll() with a filter in PHP?
hard
A. $adults = $stmt->fetchAll(fn($r) => $r['age'] > 18);