CRUD operation with PDO Database Connection

by Saquib Rizwan  February 14, 2017

Databases 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. These objects are used to setup PDO database connections.

PDO database connections

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_CUBRID Cubrid
PDO_DBLIB FreeTDS / Microsoft SQL Server / Sybase
PDO_FIREBIRD Firebird
PDO_IBM IBM DB2
PDO_INFORMIX IBM Informix Dynamic Server
PDO_MYSQL MySQL 3.x/4.x/5.x
PDO_OCI Oracle Call Interface
PDO_ODBC ODBC v3 (IBM DB2, unixODBC and win32 ODBC)
PDO_PGSQL PostgreSQL
PDO_SQLITE SQLite 3 and SQLite 2
PDO_SQLSRV Microsoft SQL Server / SQL Azure
PDO_4D 4D

 

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 prepare statement. 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. If you want speed up your PDO queries with iterators, check out this article by Michelangelo van Dam.

In this article, I introduced PDO and highlighted how you could perform CRUD actions using PDO in PHP. I also demonstrated setting up of PDO database connections. If you have questions or would like to add to the discussion, do leave a comment below.

Start Creating Web Apps on Managed Cloud Servers Now!

Easy Web App Deployment for Agencies, Developers and E-Commerce Industry

About Saquib Rizwan

Saquib is a PHP Community Expert at Cloudways - A Managed PHP Hosting Cloud Platform. He is well versed in PHP and regularly contributes to open source projects. For fun, he enjoys gaming, movies and hanging out with friends. You can email him at saquib.rizwan@cloudways.com

Stay Connected:

You Might Also Like...