Complete the code to create a new PDO instance connecting to a MySQL database.
$pdo = new PDO([1]);The correct DSN for MySQL in PDO uses the format mysql:host=hostname;dbname=database.
Complete the code to set the PDO error mode to throw exceptions.
$pdo->setAttribute(PDO::ATTR_ERRMODE, [1]);Setting error mode to PDO::ERRMODE_EXCEPTION makes PDO throw exceptions on errors, which is best for debugging and error handling.
Fix the error in the prepared statement execution by completing the missing method.
$stmt = $pdo->prepare('SELECT * FROM users WHERE id = :id'); $stmt->[1]([':id' => 1]);
The execute method runs the prepared statement with the given parameters.
Fill both blanks to fetch all rows as associative arrays after executing a prepared statement.
$stmt = $pdo->prepare('SELECT * FROM products'); $stmt->[1](); $rows = $stmt->[2](PDO::FETCH_ASSOC);
First, you execute the statement, then fetch all rows as associative arrays using fetchAll(PDO::FETCH_ASSOC).
Fill all three blanks to prepare, execute with a parameter, and fetch one row as an object.
$stmt = $pdo->[1]('SELECT * FROM orders WHERE order_id = :id'); $stmt->[2]([':id' => 10]); $order = $stmt->[3](PDO::FETCH_OBJ);
You prepare the statement, execute it with parameters, then fetch one row as an object.