php basic pdo mysql connection and CRUD example


In this article I will be explaining php basic PDO mysql connection and queries example
PDO stands for PHP Data Objects, it provides Multipledatabase access. by multiple database access it means that if we change our database from MySql to MS Access or Sqlite, we just need to make small change in database connection line, all other coding will work perfectly for any kind of database with same structure of database tables.
every thing has its advantages and disadvantages, some major advantages of PDO are

  • Multiple RDBMS
  • Parameters binding which looks clean and prevents sql injection
  • Its faster then mysql or mysqli

few disadvantages are

  • Do not have enough features as mysqli has like multiple queries
  • Only OOP implementation, no procedural

we should use PDO if having plan to change RDBMS in futues, we can use PDO for

  • PHP to MS Sql
  • PHP to MS Access
  • PHP to Oracle
  • PHP to Sybase
  • PHP to Sqlite
  • PHP to Mysql
  • PHP to any database with out changing queries or syntax

PDO Datbase Connection

we can connect to any database like MS SQL SQLite MS Access MySql etc using pdo
few examples are given below pick yours from it.

MS SQL connection

SQLite connection

MS Access connection

MySQL connection

these were different RDBMS connections you can google and find more. we have wrapped all connections in try catch to handle errors.

PDO Select Query

use the following code snippet to select data from database

PDO Update Query

we can update record(s) using the following code snippet

PDO Insert Query

we can Insert record using with PDO as the following code snippet

PDO DeleteQuery

we can Delete record(s) using with PDO as the following code snippet

these are all crud operations with PDO, I did it in the simplest way so that beginners can understand easily, customize it as you need, comment if you have any issue. and keep in touch to have these kind of articles like our facebook and google plus pages.


  • Write a Reply or Comment

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