Skip to content

Chapter 25 of 38

PDO Database Access

Use portable connections, prepared statements, fetch modes, errors, and transactions.

44 minutes 10 quick checksBy Subha Prasad
Lesson 25 of 38Course navigation

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.

Which statement best describes PDO?
Which PHP term matches this description: A consistent database-access abstraction supporting multiple drivers.
Which statement best describes PDOStatement?
Which PHP term matches this description: A prepared/executed statement and result cursor.
Which statement best describes ERRMODE_EXCEPTION?
Which PHP term matches this description: PDO mode that reports database failures as exceptions.
Which statement best describes fetch mode?
Which PHP term matches this description: The representation used for fetched rows.
Which statement best describes named placeholder?
Which PHP term matches this description: A prepared-statement marker such as :email.

0 of 10 checks passed

Your progress is saved on this device.

PDO Database Access | PHP Lesson | Subha Prasad