What is PDO

What is PDO?

What is PDO?

PDO stands for PHP Data Objects and is an interface software that allows us to access the database in a very lightweight and consistent manner. PDO is a system worthy of its name, featuring an Object Oriented Programming interface and supporting dozens of database drivers.

Some of the database drivers it supports
  1. Cubrid
  2. FreeTDS/Microsoft SQL Server / Sybase
  3. Firebird / Interbase 6
  4. IBM DB2
  5. IBM Informix Dynamic Server
  6. MySQL 3.x/4.x/5.x
  7. Oracle Call Interface
  8. PostgreSQL
  9. SQLite 3 and SQLite 2
  10. SQL Azure
When you start coding your project, if you later wish to switch from one database driver to another, you can change the driver with very few or even no modifications if you have coded your system using PDO, instead of having to completely rewrite your code.

With PDO, you can use all the features you use in systems like MySQL and MySQLi, and you can do so very simply.

Note: To use PDO, you need PHP 5.1 or later versions. 

Connecting to MYSQL with PDO

Connecting to MYSQL with PDO is quite simple, 
 $db = new PDO('mysql:host=localhost;dbname=test', $user, $pass); 
When using PDO, you should be careful to use it within a Try / Catch structure.
$dsn = 'mysql:host=localhost;dbname=test';
$user = 'dbuser';
$password = 'mypassword';
 
try {
    $db = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}