The correct syntax uses the PDO constructor with a DSN string and credentials.
Why do we use try-catch blocks with PDO connections?
ATo handle connection errors without crashing the script
BTo speed up the connection
CTo encrypt the database password
DTo automatically close the connection
✗ Incorrect
Try-catch blocks catch exceptions if the connection fails, allowing graceful error handling.
What does setting PDO::ATTR_ERRMODE to PDO::ERRMODE_EXCEPTION do?
AMakes PDO throw exceptions on errors
BDisables error reporting
CLogs errors to a file
DAutomatically retries failed queries
✗ Incorrect
This setting makes PDO throw exceptions, which helps detect and handle errors.
Which of these is NOT part of the DSN string for a MySQL PDO connection?
Acharset
Bdbname
Chost
Duser
✗ Incorrect
The username is passed separately, not inside the DSN string.
What will happen if the PDO connection fails and there is no try-catch block?
AThe connection will retry automatically
BThe script will stop with a fatal error
CThe script will ignore the error and continue
DThe database will connect anyway
✗ Incorrect
Without try-catch, a failed connection throws a fatal error stopping the script.
Explain how to set up a PDO connection to a MySQL database in PHP.
Think about the steps to connect safely and handle errors.
You got /4 concepts.
Why is it important to set PDO error mode to exceptions when connecting to a database?
Consider how errors are handled in your code.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using PDO in PHP?
easy
A. To connect to databases securely and easily
B. To create HTML pages
C. To handle user sessions
D. To send emails
Solution
Step 1: Understand PDO's role
PDO is a PHP extension designed to connect to databases in a secure and consistent way.
Step 2: Compare other options
Other options like creating HTML, handling sessions, or sending emails are unrelated to PDO.
Final Answer:
To connect to databases securely and easily -> Option A
Quick Check:
PDO = Secure database connection [OK]
Hint: PDO is for database connections only [OK]
Common Mistakes:
Confusing PDO with HTML or session functions
Thinking PDO sends emails
Assuming PDO is for file handling
2. Which of the following is the correct way to create a new PDO connection to a MySQL database named 'testdb' on localhost with username 'user' and password 'pass'?
easy
A. $pdo = new PDO('host=localhost;dbname=testdb', 'user', 'pass');
B. $pdo = new PDO('mysql:localhost;dbname=testdb', 'user', 'pass');
C. $pdo = new PDO('mysql://localhost/testdb', 'user', 'pass');
D. $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'pass');
Solution
Step 1: Check DSN format for MySQL
The correct DSN format is 'mysql:host=hostname;dbname=database_name'.
Step 2: Verify options
$pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'pass'); uses the correct DSN format with host and dbname separated by semicolon and includes username and password.
Final Answer:
$pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'pass'); -> Option D
Quick Check:
DSN format = 'mysql:host=...;dbname=...' [OK]
Hint: DSN must start with 'mysql:' and use semicolons [OK]
Common Mistakes:
Omitting 'mysql:' prefix
Using slashes instead of semicolons
Missing 'host=' keyword
3. What will be the output of this PHP code snippet?
The DSN string is correct and includes host and dbname properly.
Step 2: Check error mode setting
PDO::ATTR_ERRMODE and PDO::ERRMODE_EXCEPTION are valid constants to enable exceptions on errors.
Step 3: Verify syntax
All lines end with semicolons and try-catch is properly structured.
Final Answer:
No error, code is correct -> Option C
Quick Check:
Correct DSN and error mode = no error [OK]
Hint: Check constants and syntax carefully [OK]
Common Mistakes:
Using wrong error mode constants
Forgetting semicolons
Miswriting DSN string
5. You want to connect to a MySQL database using PDO and ensure that any connection error throws an exception. Which code snippet correctly sets up the connection and error handling?