Lesson content
Read, practise, then check your understanding
PDO presents a consistent interface across supported database drivers, though SQL dialects remain database-specific. Configure exceptions, native prepares where appropriate, and deliberate fetch modes.
Practical example
<?php
$pdo = new PDO($dsn, $user, $password, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$stmt = $pdo->prepare('SELECT name FROM users WHERE email = :email');
$stmt->execute(['email' => $email]);
$name = $stmt->fetchColumn();
Close cursors when needed and release connections by dropping references. Wrap multi-step writes in transactions with rollback in catch. Do not expose PDO across the whole application; repositories/services should express domain operations.
Knowledge check
Answer every question correctly to complete this chapter.
0 of 10 checks passed
Your progress is saved on this device.