Challenge - 5 Problems
PDO Fetch Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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); ?>
Attempts:
2 left
💡 Hint
PDO::FETCH_ASSOC returns an associative array indexed by column names.
✗ Incorrect
The fetch mode PDO::FETCH_ASSOC returns an array indexed by column names only, so the output contains keys 'id' and 'name' with their respective values.
❓ Predict Output
intermediate2: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); ?>
Attempts:
2 left
💡 Hint
PDO::FETCH_NUM returns a numeric array indexed by column numbers.
✗ Incorrect
The fetch mode PDO::FETCH_NUM returns an array indexed by column numbers starting at 0, so the output contains keys 0 and 1 with values 20 and 'Pencil'.
❓ Predict Output
advanced2: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; ?>
Attempts:
2 left
💡 Hint
PDO::FETCH_OBJ returns an anonymous object with properties named after columns.
✗ Incorrect
Using PDO::FETCH_OBJ returns an object where column names are properties. Accessing $result->name prints 'John'.
❓ Predict Output
advanced2: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; ?>
Attempts:
2 left
💡 Hint
PDO::FETCH_CLASS requires a class name as the second argument.
✗ Incorrect
When using PDO::FETCH_CLASS, you must specify the class name as the second argument to fetch(). Omitting it causes a fatal error.
🧠 Conceptual
expert2: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?
Attempts:
2 left
💡 Hint
This fetch mode combines numeric and associative keys in the result array.
✗ Incorrect
PDO::FETCH_BOTH returns an array indexed by both column names and column numbers, so you get both numeric and associative keys.