🐘 PHP Intermediate

What is PDO in PHP?

Why Interviewers Ask This

This tests whether you can apply PHP knowledge to real-world scenarios. Interviewers are looking for clarity of thought and evidence that you've encountered this in production code.

Answer

PDO (PHP Data Objects) is a database abstraction layer that provides a consistent interface for accessing different database systems (MySQL, PostgreSQL, SQLite, MSSQL, etc.). By changing the DSN (Data Source Name), you can switch databases with minimal code changes. Key features: prepared statements for SQL injection prevention, transactions (beginTransaction(), commit(), rollBack()), multiple fetch modes (PDO::FETCH_ASSOC, PDO::FETCH_OBJ, PDO::FETCH_CLASS), and error handling via exceptions (PDO::ERRMODE_EXCEPTION). Always set error mode to ERRMODE_EXCEPTION: $pdo = new PDO($dsn, $user, $pass, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);.

Pro Tip

Demonstrate both theoretical understanding and practical experience. Say what it is, then give an example of how you actually used it in a PHP codebase.