An Introduction To PHP Data Objects (PDO)

An Introduction To PHP Data Objects

D
atabases are everywhere and there is no practical PHP application that could exist without a database. From the very beginning, PHP offers several ways of interfacing with all popular DBMS. For instance, two popular ways of interfacing with MySQL based databases are mysql and mysqli.

Over years, databases have come a long way and now several different vendors offer popular DBMS that power modern PHP apps. To standardize and streamline development practices, PHP introduced PHP Data Objects (PDO) in PHP 5.1
introduction to pdo in php
PDO is a database access layer which provides a fast and consistent interface for accessing and managing databases in PHP applications.  Every DBMS has specific PDO driver(s) that must be installed when you are using PDO in PHP applications.

Supported Databases

Driver nameSupported Database
PDO_CUBRIDCubrid
PDO_DBLIBFreeTDS / Microsoft SQL Server / Sybase
PDO_FIREBIRDFirebird
PDO_IBMIBM DB2
PDO_INFORMIXIBM Informix Dynamic Server
PDO_MYSQLMySQL 3.x/4.x/5.x
PDO_OCIOracle Call Interface
PDO_ODBCODBC v3 (IBM DB2, unixODBC and win32 ODBC)
PDO_PGSQLPostgreSQL
PDO_SQLITESQLite 3 and SQLite 2
PDO_SQLSRVMicrosoft SQL Server / SQL Azure
PDO_4D4D

By default, PHP has PDO_SQLite driver installed. However, if you wish to work with other databases, you must first install the relevant driver.
in order to check what drivers are installed on your system, create a new PHP file and add the following code snippet to it:

Working With PDO

PDO replaces all previous database interaction approaches. Using PDO, you could easily perform CRUD and related DBMS operations. In effect, PDO acts as a layer that separates database related operations from the rest of the code.

Connectivity

One of the most important benefits of PDO is the simple and very straightforward database connectivity. Consider the following code snippet that is used to set up connections with the database. Note that when the underlying DBMS changes, the only change that you need to make is the database type.
In the above code snippet, notice that the DBMS is MySQL. However, if the DBMS changes to MS SQL Server, the only change will be the replacement of mysql with mssql.
Note: PDO can handle exceptions. Therefore, always wrap its operation in a try and catch block.

Creating a Table With PDO

In order to create a table, first declare a query string and then execute it with exec function as no data will be returned.

Inserting Data With PDO

In order to insert data into a table using PDO, first prepare the query using preparestatement. Next, this query is executed with the execute function. Note that this practice prevents SQL injection attacks.

Select Data With PDO

In order to select data, first create a query string and then execute it in a for each loop to fetch records from the table.

Update Data With PDO

In order to update a record in the table, first declare a query string and then execute it with exec function.

Delete Data With PDO

In order to delete a record from the table, first declare a query string and then execute it with exec function.

Conclusion

PDO is the data accessing layer that greatly eases the process of connecting and working with databases. Perhaps, the best thing about PDO is the streamlined process of database migration. In this article, I introduced PDO and highlighted how you could perform CRUD actions using PDO in PHP. If you have questions or would like to add to the discussion, do leave a comment below.

Comments