Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to fetch one row from the PDO statement.
PHP
<?php $stmt = $pdo->query('SELECT * FROM users'); $row = $stmt->[1](); print_r($row); ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using fetchAll() when only one row is needed.
Using execute() which runs a statement but does not fetch data.
✗ Incorrect
The fetch() method fetches the next row from the result set.
2fill in blank
mediumComplete the code to fetch all rows from the PDO statement.
PHP
<?php $stmt = $pdo->query('SELECT * FROM products'); $rows = $stmt->[1](); foreach ($rows as $row) { echo $row['name'] . "\n"; } ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using fetch() when all rows are needed.
Using execute() which does not fetch data.
✗ Incorrect
The fetchAll() method fetches all rows at once as an array.
3fill in blank
hardFix the error in fetching rows as associative arrays.
PHP
<?php $stmt = $pdo->query('SELECT id, email FROM users'); $rows = $stmt->fetchAll([1]); foreach ($rows as $row) { echo $row['email'] . "\n"; } ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using FETCH_NUM which returns numeric keys.
Using FETCH_OBJ which returns objects instead of arrays.
✗ Incorrect
Use PDO::FETCH_ASSOC to fetch rows as associative arrays with column names as keys.
4fill in blank
hardFill both blanks to fetch rows as objects and print a property.
PHP
<?php $stmt = $pdo->query('SELECT id, username FROM users'); $rows = $stmt->[1]([2]); foreach ($rows as $user) { echo $user->username . "\n"; } ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using fetch() instead of fetchAll() to get all rows.
Using FETCH_ASSOC which returns arrays, not objects.
✗ Incorrect
Use fetchAll(PDO::FETCH_OBJ) to get all rows as objects.
5fill in blank
hardFill all three blanks to fetch one row as a numeric array and print the first column.
PHP
<?php $stmt = $pdo->query('SELECT id, name FROM categories'); $row = $stmt->[1]([2]); echo $row[[3]]; ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using fetchAll() when only one row is needed.
Using FETCH_ASSOC which returns associative arrays.
Using wrong index to access the first column.
✗ Incorrect
Use fetch(PDO::FETCH_NUM) to get one row as a numeric array, then access the first column with index 0.