Complete the code to fetch one row from the PDO statement.
<?php $stmt = $pdo->query('SELECT * FROM users'); $row = $stmt->[1](); print_r($row); ?>
The fetch() method fetches the next row from the result set.
Complete the code to fetch all rows from the PDO statement.
<?php $stmt = $pdo->query('SELECT * FROM products'); $rows = $stmt->[1](); foreach ($rows as $row) { echo $row['name'] . "\n"; } ?>
The fetchAll() method fetches all rows at once as an array.
Fix the error in fetching rows as associative arrays.
<?php $stmt = $pdo->query('SELECT id, email FROM users'); $rows = $stmt->fetchAll([1]); foreach ($rows as $row) { echo $row['email'] . "\n"; } ?>
Use PDO::FETCH_ASSOC to fetch rows as associative arrays with column names as keys.
Fill both blanks to fetch rows as objects and print a property.
<?php $stmt = $pdo->query('SELECT id, username FROM users'); $rows = $stmt->[1]([2]); foreach ($rows as $user) { echo $user->username . "\n"; } ?>
Use fetchAll(PDO::FETCH_OBJ) to get all rows as objects.
Fill all three blanks to fetch one row as a numeric array and print the first column.
<?php $stmt = $pdo->query('SELECT id, name FROM categories'); $row = $stmt->[1]([2]); echo $row[[3]]; ?>
Use fetch(PDO::FETCH_NUM) to get one row as a numeric array, then access the first column with index 0.
