Which of the following is the main reason PDO is considered the standard for database access in PHP?
Think about how PDO helps when switching databases.
PDO provides a consistent interface to many different databases, so you can switch databases without rewriting all your code.
What will be the output of this PHP code using PDO prepared statements?
<?php $pdo = new PDO('sqlite::memory:'); $pdo->exec('CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)'); $stmt = $pdo->prepare('INSERT INTO users (name) VALUES (:name)'); $stmt->execute(['name' => 'Alice']); $stmt->execute(['name' => 'Bob']); $count = $pdo->query('SELECT COUNT(*) FROM users')->fetchColumn(); echo $count; ?>
Count how many rows were inserted.
The code inserts two users, so the count is 2.
What error behavior will this PDO code produce?
<?php $pdo = new PDO('sqlite::memory:'); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); try { $pdo->exec('INVALID SQL'); echo 'Success'; } catch (PDOException $e) { echo 'Error caught'; } ?>
Check how PDO handles errors with ERRMODE_EXCEPTION.
PDO throws an exception on invalid SQL when ERRMODE_EXCEPTION is set, so the catch block runs.
Which feature of PDO helps prevent SQL injection attacks?
Think about how user input is handled safely.
Prepared statements separate code from data, preventing malicious input from changing the query structure.
Which statement best explains how PDO improves database portability in PHP applications?
Consider how PDO abstracts database differences.
PDO offers a consistent interface for different databases, so you only need to change the connection string, not the query code.