Bird
Raised Fist0
PHPprogramming~20 mins

Fetch modes and styles in PHP - Practice Problems & Coding Challenges

Choose your learning style10 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 PDO fetch mode code?
Consider the following PHP code snippet using PDO to fetch data from a database. What will be the output of the print_r statement?
PHP
<?php
$pdo = new PDO('sqlite::memory:');
$pdo->exec("CREATE TABLE users (id INTEGER, name TEXT);");
$pdo->exec("INSERT INTO users VALUES (1, 'Alice'), (2, 'Bob');");
$stmt = $pdo->query("SELECT * FROM users WHERE id = 1");
$result = $stmt->fetch(PDO::FETCH_ASSOC);
print_r($result);
?>
AArray ( [id] => 1 [name] => Alice )
BArray ( [0] => 1 [1] => Alice )
CArray ( [id] => 1 [0] => 1 [name] => Alice [1] => Alice )
DNULL
Attempts:
2 left
💡 Hint
PDO::FETCH_ASSOC returns an associative array indexed by column names.
❓ Predict Output
intermediate
2:00remaining
What does this PHP PDO fetch style code output?
Given this PHP code using PDO, what will be the output of the print_r statement?
PHP
<?php
$pdo = new PDO('sqlite::memory:');
$pdo->exec("CREATE TABLE products (id INTEGER, name TEXT);");
$pdo->exec("INSERT INTO products VALUES (10, 'Pen'), (20, 'Pencil');");
$stmt = $pdo->query("SELECT * FROM products WHERE id = 20");
$result = $stmt->fetch(PDO::FETCH_NUM);
print_r($result);
?>
AArray ( [0] => 20 [1] => Pencil )
BArray ( [id] => 20 [name] => Pencil )
CArray ( [id] => 20 [0] => 20 [name] => Pencil [1] => Pencil )
Dfalse
Attempts:
2 left
💡 Hint
PDO::FETCH_NUM returns a numeric array indexed by column numbers.
❓ Predict Output
advanced
2:00remaining
What is the output of this PHP PDO fetch with FETCH_OBJ?
Look at this PHP code using PDO with fetch mode FETCH_OBJ. What will be the output of the echo statement?
PHP
<?php
$pdo = new PDO('sqlite::memory:');
$pdo->exec("CREATE TABLE employees (id INTEGER, name TEXT);");
$pdo->exec("INSERT INTO employees VALUES (5, 'John');");
$stmt = $pdo->query("SELECT * FROM employees WHERE id = 5");
$result = $stmt->fetch(PDO::FETCH_OBJ);
echo $result->name;
?>
AArray
BJohn
C5
DNotice: Trying to get property of non-object
Attempts:
2 left
💡 Hint
PDO::FETCH_OBJ returns an anonymous object with properties named after columns.
❓ Predict Output
advanced
2:00remaining
What error does this PHP PDO fetch code produce?
What error will this PHP code produce when executed?
PHP
<?php
$pdo = new PDO('sqlite::memory:');
$pdo->exec("CREATE TABLE items (id INTEGER, name TEXT);");
$pdo->exec("INSERT INTO items VALUES (1, 'Book');");
$stmt = $pdo->query("SELECT * FROM items WHERE id = 1");
$result = $stmt->fetch(PDO::FETCH_CLASS);
echo $result->name;
?>
AFatal error: Uncaught ArgumentCountError: PDO::fetch() expects at least 1 parameter
BWarning: PDO::fetch() expects parameter 1 to be int, string given
CBook
DFatal error: Class name must be specified when using PDO::FETCH_CLASS
Attempts:
2 left
💡 Hint
PDO::FETCH_CLASS requires a class name as the second argument.
🧠 Conceptual
expert
2:00remaining
Which fetch mode returns both numeric and associative keys?
In PHP PDO, which fetch mode returns an array containing both numeric indexes and associative keys for the columns?
APDO::FETCH_NUM
BPDO::FETCH_ASSOC
CPDO::FETCH_BOTH
DPDO::FETCH_OBJ
Attempts:
2 left
💡 Hint
This fetch mode combines numeric and associative keys in the result array.

Practice

(1/5)
1. Which PHP PDO fetch mode returns each row as an associative array with column names as keys?
easy
A. PDO::FETCH_OBJ
B. PDO::FETCH_NUM
C. PDO::FETCH_ASSOC
D. PDO::FETCH_COLUMN

Solution

  1. Step 1: Understand fetch modes

    PDO::FETCH_ASSOC returns rows as associative arrays with column names as keys.
  2. Step 2: Compare with other modes

    PDO::FETCH_NUM returns numeric arrays, PDO::FETCH_OBJ returns objects, PDO::FETCH_COLUMN returns a single column value.
  3. Final Answer:

    PDO::FETCH_ASSOC -> Option C
  4. Quick Check:

    Associative array = PDO::FETCH_ASSOC [OK]
Hint: Associative arrays use PDO::FETCH_ASSOC for column-name keys [OK]
Common Mistakes:
  • Confusing PDO::FETCH_NUM with associative arrays
  • Using PDO::FETCH_OBJ when array is needed
  • Thinking PDO::FETCH_COLUMN returns full rows
2. Which of the following is the correct syntax to fetch a single row as an object using PDO?
easy
A. $stmt->fetch(PDO::FETCH_OBJ);
B. $stmt->fetch(PDO::FETCH_ASSOC);
C. $stmt->fetchAll(PDO::FETCH_OBJ);
D. $stmt->fetchColumn();

Solution

  1. Step 1: Identify fetch method for single row as object

    fetch() returns one row; PDO::FETCH_OBJ returns it as an object.
  2. Step 2: Check other options

    fetchAll() returns all rows, fetchColumn() returns a single column value, fetch(PDO::FETCH_ASSOC) returns an associative array.
  3. Final Answer:

    $stmt->fetch(PDO::FETCH_OBJ); -> Option A
  4. Quick Check:

    Single row object = fetch(PDO::FETCH_OBJ) [OK]
Hint: Use fetch(PDO::FETCH_OBJ) for one row as object [OK]
Common Mistakes:
  • Using fetchAll() when only one row is needed
  • Using fetchColumn() to get full row
  • Confusing FETCH_ASSOC with FETCH_OBJ
3. What will be the output of the following PHP code?
$stmt = $pdo->query('SELECT id, name FROM users LIMIT 1');
$row = $stmt->fetch(PDO::FETCH_NUM);
echo $row[0] . ', ' . $row[1];
medium
A. id, name
B. 1, Alice
C. Array, Array
D. Error: Undefined index

Solution

  1. Step 1: Understand PDO::FETCH_NUM

    fetch(PDO::FETCH_NUM) returns a numeric array with column values indexed 0,1,...
  2. Step 2: Analyze echo statement

    $row[0] is the first column (id), $row[1] is the second column (name). Assuming first user has id=1 and name='Alice', output is '1, Alice'.
  3. Final Answer:

    1, Alice -> Option B
  4. Quick Check:

    Numeric array indexes = 0,1 values [OK]
Hint: FETCH_NUM returns numeric keys, use indexes 0,1 for columns [OK]
Common Mistakes:
  • Expecting column names as keys with FETCH_NUM
  • Using associative keys with numeric array
  • Assuming fetchColumn returns full row
4. Identify the error in this PHP code snippet that fetches all user names as a single column array:
$stmt = $pdo->query('SELECT name FROM users');
$names = $stmt->fetch(PDO::FETCH_COLUMN);
print_r($names);
medium
A. print_r cannot print arrays
B. PDO::FETCH_COLUMN is invalid fetch mode
C. Query should select id, not name
D. fetch() returns only one value, should use fetchAll()

Solution

  1. Step 1: Understand fetch() vs fetchAll()

    fetch() returns a single row or value, fetchAll() returns all rows.
  2. Step 2: Check usage of FETCH_COLUMN

    FETCH_COLUMN fetches a single column value per row; to get all names, fetchAll(PDO::FETCH_COLUMN) is needed.
  3. Final Answer:

    fetch() returns only one value, should use fetchAll() -> Option D
  4. Quick Check:

    fetchAll(PDO::FETCH_COLUMN) gets all column values [OK]
Hint: Use fetchAll(PDO::FETCH_COLUMN) to get all values of one column [OK]
Common Mistakes:
  • Using fetch() instead of fetchAll() for multiple rows
  • Thinking FETCH_COLUMN fetches all rows automatically
  • Misunderstanding print_r usage
5. You want to fetch all rows from a database as objects but only include rows where the 'active' column is true. Which PHP code snippet correctly applies this filter using fetch modes and styles?
hard
A. $rows = $stmt->fetchAll(PDO::FETCH_OBJ); $activeRows = array_filter($rows, fn($r) => $r->active);
B. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); foreach ($rows as $r) { if ($r['active']) echo $r; }
C. $rows = $stmt->fetchAll(PDO::FETCH_COLUMN); $activeRows = array_filter($rows, fn($r) => $r->active);
D. $rows = $stmt->fetch(PDO::FETCH_OBJ); while ($rows->active) { echo $rows; }

Solution

  1. Step 1: Fetch all rows as objects

    fetchAll(PDO::FETCH_OBJ) returns an array of objects, each representing a row.
  2. Step 2: Filter rows where 'active' is true

    array_filter with a callback checks the 'active' property on each object, keeping only those with true.
  3. Step 3: Check other options

    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); foreach ($rows as $r) { if ($r['active']) echo $r; } uses associative arrays but does not collect filtered rows properly; $rows = $stmt->fetchAll(PDO::FETCH_COLUMN); $activeRows = array_filter($rows, fn($r) => $r->active); uses FETCH_COLUMN which returns single column values, not objects; $rows = $stmt->fetch(PDO::FETCH_OBJ); while ($rows->active) { echo $rows; } misuses fetch() and while loop.
  4. Final Answer:

    $rows = $stmt->fetchAll(PDO::FETCH_OBJ); $activeRows = array_filter($rows, fn($r) => $r->active); -> Option A
  5. Quick Check:

    fetchAll(PDO::FETCH_OBJ) + array_filter for property check [OK]
Hint: Use fetchAll(PDO::FETCH_OBJ) then array_filter by property [OK]
Common Mistakes:
  • Using fetch() instead of fetchAll() for multiple rows
  • Using FETCH_COLUMN when objects are needed
  • Trying to filter before fetching all rows
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard