🐘 PHP Intermediate

What is PDO in PHP?

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]);.