PDO select, insert, delete, update mysql database in PHP



In this tutorial we are creating an example to do database operations Create, Read, Update and Delete (CRUD) in PHP using PDO connection. In a previous tutorial, we have already learned about how to do database CRUD using MySQLi

In this example, we have taken a posts table to do CRUD via PDO. The SQL script for this table is given with the source code download. We create PDO connection in a common file and include this file wherever the database connection is required.

Creating PDO Connection

The following code shows how to create PDO connection in PHP.

Create New Row in Database Table

The code shows the HTML for the add form and the PHP script to be executed to get PDO connection to perform Create operation. On submitting this form, the fields are added in a new row.


After creating a new row in the database table, we are going to fetch all records via PDO connection and list them in a list page. The code is,

Update Database Row

In update form, we get values from the database for a particular record and populate those values in the form fields. The code shows update query to be executed using PDO connection to do database update.

Deleting Row using PDO

The following PHP code shows how to fire delete query using PDO connection.


Leave A Reply

Your email address will not be published.