0
0
PHPprogramming~10 mins

Why PDO is the standard in PHP - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a new PDO instance connecting to a MySQL database.

PHP
$pdo = new PDO([1]);
Drag options to blanks, or click blank then click option'
A"mysql:host=localhost;dbname=testdb"
B"pgsql:host=localhost;dbname=testdb"
C"mysql://localhost/testdb"
D"sqlite:testdb.sqlite"
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect DSN format like 'mysql://localhost/testdb'.
Confusing MySQL DSN with PostgreSQL or SQLite.
2fill in blank
medium

Complete the code to set the PDO error mode to throw exceptions.

PHP
$pdo->setAttribute(PDO::ATTR_ERRMODE, [1]);
Drag options to blanks, or click blank then click option'
APDO::ERRMODE_WARNING
BPDO::ERRMODE_EXCEPTION
CPDO::ERRMODE_SILENT
DPDO::ERRMODE_FATAL
Attempts:
3 left
💡 Hint
Common Mistakes
Using ERRMODE_SILENT which hides errors.
Using ERRMODE_WARNING which only raises warnings.
3fill in blank
hard

Fix the error in the prepared statement execution by completing the missing method.

PHP
$stmt = $pdo->prepare('SELECT * FROM users WHERE id = :id');
$stmt->[1]([':id' => 1]);
Drag options to blanks, or click blank then click option'
Afetch
Brun
Cquery
Dexecute
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'query' which does not accept parameters.
Using 'fetch' which retrieves data but does not execute the statement.
4fill in blank
hard

Fill both blanks to fetch all rows as associative arrays after executing a prepared statement.

PHP
$stmt = $pdo->prepare('SELECT * FROM products');
$stmt->[1]();
$rows = $stmt->[2](PDO::FETCH_ASSOC);
Drag options to blanks, or click blank then click option'
Aexecute
BfetchAll
Cfetch
Dquery
Attempts:
3 left
💡 Hint
Common Mistakes
Calling fetch before execute.
Using 'query' instead of 'execute' for prepared statements.
5fill in blank
hard

Fill all three blanks to prepare, execute with a parameter, and fetch one row as an object.

PHP
$stmt = $pdo->[1]('SELECT * FROM orders WHERE order_id = :id');
$stmt->[2]([':id' => 10]);
$order = $stmt->[3](PDO::FETCH_OBJ);
Drag options to blanks, or click blank then click option'
Aprepare
Bexecute
Cfetch
Dquery
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'query' instead of 'prepare' for parameterized queries.
Calling 'fetchAll' when only one row is needed.