What is PDO? Why PDO should be used in PHP?

|
| By Webner

PDO stands for PHP Data Objects. PDO is a consistent way to access databases.
Today, mostly websites or desktop based applications store their data in databases. Therefore, PHP has also provided the way to connect with databases.

PHP supports following ways to connect to the database:

1. Mysql
2. Mysqli (Mysql Improved)
3. PDO

Mysql and Mysqli work with Mysql Database only.

For Mysql Database connection, we can use both Mysqli and PDO. Mysql is now deprecated for higher versions and will be removed soon. So, for PHP connection with Mysql, we should prefer to use Mysqli or PDO.

To know about how to connect with database using PHP with above approaches, please visit the link:

http://www.w3schools.com/php/php_mysql_connect.asp

Following is the way for making connection with database with above 3 approaches:

1. $link = mysqli_connect($servername, $username, $password, $databasename); // Mysqli Procedural
$link = new mysqli($servername, $username, $password, $databasename); // Mysqli Object-Oriented approach

2. $link = mysql_connect(‘localhost’, ‘user’, ‘pass’, $databasename); // Mysql approach

3. $link = new PDO(‘mysql:host=servername;dbname=your_database_name’, $username, $password); // PDO approach

Now, we should know what is the difference between Mysqli and PDO. The first main difference is: Mysqli supports only Mysql database connection and PDO supports connection with other databases too including Mysql database. With PDO, it is easy to work with prepared statements of queries.

PDO is more like a data access layer which provides an abstraction to differences in functions for accessing different databases. PDO provides a portability among different databases. Suppose you are using Mysql database and now want to switch to some other database like Postgres Sql or ODBC, then it will become easy to switch to another database with PDO. PDO uses same functions for all databases, therefore it is portable for switching between databases. Only connection code needs to be changed, rest all code remains same. If we use Mysqli approach to connecting with Mysql database, later on, requirement changed to switch to Postgres Sql or another database, then we need to make a lot of changes in code for working with PDO functions. If we were already using PDO connection approach, the there is no need for coding changes, we only need to change database type in connection coding like below:

$link = new PDO('mysql:host=servername;dbname=your_database_name', $username, $password);		// PDO approach for connection with Mysql
$link = new PDO('pgsql:host=servername;dbname=your_database_name', $username, $password);		// PDO approach for connection with Postgres

PDO supports following databases to be connected with PHP:

* Cubrid
* FreeTDS / Microsoft SQL Server / Sybase
* Firebird
* IBM DB2
* IBM Informix Dynamic Server
* MySQL 3.x/4.x/5.x
* Oracle Call Interface
* ODBC v3 (IBM DB2, unixODBC and win32 ODBC)
* PostgreSQL
* SQLite 3 and SQLite 2
* Microsoft SQL Server / SQL Azure
* 4D

Now, we know about PDO and know why it is better to use PDO.

Leave a Reply

Your email address will not be published. Required fields are marked *