Advertisement
Beginner PHP, PDO & Databases

What is PDO in PHP, and what advantages does it provide over older database extensions like mysqli for interacting with a database?

Short answer

PDO, or PHP Data Objects, provides a consistent, unified interface for accessing many different database systems, including MySQL, PostgreSQL, and SQLite, meaning the same general code structure works across different databases with only minor changes, unlike the older mysqli…

Save PHP mock interview
Advertisement
AModel answer
PDO, or PHP Data Objects, provides a consistent, unified interface for accessing many different database systems, including MySQL, PostgreSQL, and SQLite, meaning the same general code structure works across different databases with only minor changes, unlike the older mysqli extension which only works with MySQL, and PDO also provides built in support for prepared statements, which is essential for writing secure database queries that properly protect against SQL injection.
$pdo = new PDO('mysql:host=localhost;dbname=myapp', 'username', 'password');
$stmt = $pdo->query('SELECT * FROM users');
Real-world example
A company building an application that might need to support multiple different database systems for different clients chooses PDO specifically for its consistent interface across MySQL, PostgreSQL, and other supported databases.
Likely follow-ups
  • What database systems does PDO support besides MySQL and PostgreSQL?
  • What is the difference between PDO and mysqli in terms of object oriented versus procedural style?
Related topics