0
0
PHPprogramming~20 mins

Fetching results (fetch, fetchAll) in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PDO Fetch Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this PHP code using fetch()?

Consider the following PHP code that queries a database and fetches one row:

$stmt = $pdo->query('SELECT id, name FROM users ORDER BY id LIMIT 1');
$row = $stmt->fetch(PDO::FETCH_ASSOC);
print_r($row);

What will be printed?

PHP
$stmt = $pdo->query('SELECT id, name FROM users ORDER BY id LIMIT 1');
$row = $stmt->fetch(PDO::FETCH_ASSOC);
print_r($row);
AArray with numeric keys 0 and 1 containing the first user's data
BArray with keys 'id' and 'name' containing the first user's data
CBoolean false
DAn error message about fetchAll()
Attempts:
2 left
💡 Hint

fetch() returns one row as an associative array if you use PDO::FETCH_ASSOC.

Predict Output
intermediate
2:00remaining
What does fetchAll() return in this PHP example?

Given this PHP code snippet:

$stmt = $pdo->query('SELECT id, name FROM users');
$rows = $stmt->fetchAll(PDO::FETCH_NUM);
print_r($rows);

What is the structure of $rows?

PHP
$stmt = $pdo->query('SELECT id, name FROM users');
$rows = $stmt->fetchAll(PDO::FETCH_NUM);
print_r($rows);
AAn array of arrays with numeric keys for each row
BAn array of associative arrays with column names as keys
CA single associative array with all rows merged
DBoolean false
Attempts:
2 left
💡 Hint

PDO::FETCH_NUM returns numeric keys for columns.

Predict Output
advanced
2:00remaining
What error does this PHP code raise?

Analyze this PHP code snippet:

$stmt = $pdo->query('SELECT id, name FROM users');
$row = $stmt->fetchAll(PDO::FETCH_ASSOC);
print_r($row[0]['nonexistent_column']);

What error will occur?

PHP
$stmt = $pdo->query('SELECT id, name FROM users');
$row = $stmt->fetchAll(PDO::FETCH_ASSOC);
print_r($row[0]['nonexistent_column']);
ANo error, prints null
BFatal error: Call to undefined method fetchAll()
CTypeError because fetchAll returns boolean
DUndefined index notice for 'nonexistent_column'
Attempts:
2 left
💡 Hint

Accessing an array key that does not exist triggers a notice.

Predict Output
advanced
2:00remaining
What is the output of this PHP code using fetch() with PDO::FETCH_OBJ?

Consider this PHP code:

$stmt = $pdo->query('SELECT id, name FROM users ORDER BY id LIMIT 1');
$user = $stmt->fetch(PDO::FETCH_OBJ);
echo $user->name;

What will be printed?

PHP
$stmt = $pdo->query('SELECT id, name FROM users ORDER BY id LIMIT 1');
$user = $stmt->fetch(PDO::FETCH_OBJ);
echo $user->name;
AThe name of the first user as a string
BAn array containing the user's name
CBoolean false
DFatal error: Cannot access property on non-object
Attempts:
2 left
💡 Hint

PDO::FETCH_OBJ returns an object with properties matching column names.

🧠 Conceptual
expert
2:00remaining
How many items are in the array returned by fetchAll() after this query?

Given a table products with 5 rows, what is the number of elements in the array returned by this PHP code?

$stmt = $pdo->query('SELECT * FROM products');
$allProducts = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo count($allProducts);
A0
B1
C5
DAn error occurs
Attempts:
2 left
💡 Hint

fetchAll() returns all rows as an array of arrays.