0
0
PHPprogramming~20 mins

Fetch modes and styles 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 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.