0
0
PHPprogramming~20 mins

PDO connection setup in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PDO Connection 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 PDO connection code?

Consider the following PHP code that tries to connect to a MySQL database using PDO. What will it output if the connection is successful?

PHP
<?php
try {
  $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'pass');
  echo 'Connected successfully';
} catch (PDOException $e) {
  echo 'Connection failed: ' . $e->getMessage();
}
?>
ANo output
BConnected successfully
CFatal error: Uncaught Error: Class 'PDO' not found
DConnection failed: SQLSTATE[HY000] [1045] Access denied for user 'user'@'localhost' (using password: YES)
Attempts:
2 left
💡 Hint

If the credentials and database exist and are correct, the try block runs without error.

Predict Output
intermediate
2:00remaining
What error does this PDO connection code raise?

What error will this PHP code produce when trying to connect to a MySQL database with an invalid DSN string?

PHP
<?php
try {
  $pdo = new PDO('mysql:host=localhost;port=wrongport;dbname=testdb', 'user', 'pass');
  echo 'Connected';
} catch (PDOException $e) {
  echo $e->getMessage();
}
?>
ASQLSTATE[HY000] [2002] No such file or directory
BSQLSTATE[HY000] [1045] Access denied for user 'user'@'localhost' (using password: YES)
CFatal error: Uncaught PDOException: could not find driver
DConnected
Attempts:
2 left
💡 Hint

Using an invalid port in the DSN causes a connection failure related to the network or socket.

🔧 Debug
advanced
2:00remaining
Why does this PDO connection code cause a fatal error?

Examine this PHP code snippet. Why does it cause a fatal error?

PHP
<?php
$pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'pass');
echo $pdo->connect_error;
?>
APDO objects do not have a 'connect_error' property, causing a fatal error
BThe DSN string is invalid, causing a connection failure
CThe code is missing a try-catch block, causing a syntax error
DThe username or password is incorrect, causing an exception
Attempts:
2 left
💡 Hint

PDO does not use 'connect_error' like mysqli does.

📝 Syntax
advanced
2:00remaining
Which option correctly sets PDO to throw exceptions on error?

Which of the following code snippets correctly sets the PDO error mode to throw exceptions?

A$pdo->setErrorMode(PDO::ERRMODE_EXCEPTION);
B$pdo->setAttribute(PDO::ERRMODE_EXCEPTION, PDO::ATTR_ERRMODE);
C$pdo->setAttribute(PDO::ATTR_ERRMODE, 'PDO::ERRMODE_EXCEPTION');
D$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Attempts:
2 left
💡 Hint

The method is setAttribute with the error mode constant as the second argument.

🚀 Application
expert
2:00remaining
How many items are in the resulting array after this PDO fetchAll call?

Given this PHP code connecting with PDO and fetching all rows, how many items will be in the $rows array?

PHP
<?php
$pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'pass');
$stmt = $pdo->query('SELECT * FROM users WHERE age > 30');
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo count($rows);
?>
A1, because fetchAll returns only the first row
B0, because fetchAll returns an empty array always
CThe number of users with age greater than 30 in the database
DAn error, because fetchAll requires a parameter
Attempts:
2 left
💡 Hint

fetchAll returns all rows matching the query as an array.