PHP Database Class PDO

PHP database class is to facilitate PHP/MYSQL development. It propose convenient methods and easy way to access the database by reducing your code and reducing your time to write code in a lengthy manner. It makes the programmer to handle CRUD(Create Read Update Delete) operations very easily.It also provides the debugging facilities so that programmer can display the request and resulting table by just adding a parameter to the method of this class. Queries are aitomatically debugged when there is an errror. This php database class is bulid using PDO and BindParams so its SQL injection safe. Try to use this in all your application so that you can finish your work in an easy and simple manner.

Why we have to use PHP database Class?

PHP database class prevent you from writting more code, you can write the code in a single line instead of writing more than 10 line to execute the simple fetch query in PDO.

The first important thing is to configure the database. Follow the below step to cofigure the PHP Database class.

  • 1. Go to Db.php
  • 2. Edit the $_host, $_user, $_pass, $_db and provide your hostname, username, password and database

Fetch data with PDO

//connect to database
        $con = new PDO("mysql:host=localhost;dbname=test","root","password");
    }catch (PDOException $e){
    //prepare the query
    $stmt = $con->prepare("SELECT * FROM table WHERE password=?");
    //bind value to prevent from SQL injection
    $stmt->bindValue(1, "password");
    //execute the query
    //fetch the result
    $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
    //display the result
    print "
    print "

Fetch data in PHP Database Class

//fetch result
$con = Db::fetch("test",array("password" => "password"),array("="));
//display result

Starting a connection

    require_once "./class/Db.php"; //include PHP database CLass

Checking error

This method check query was successfull or not. It return false when query was successful with not error. Return true when query failed
require_once "./class/Db.php"; //include PHP database CLass
    if(Db::getError() == false){
        //query was successful
       //query was not succesfull

Inserting a record

Suppose consider the table name user, in there we have to store the username and password.
require_once "./class/Db.php"; //include PHP database CLass
//insert in user table
    "username" => $username,
    "password" => $password,
    "country"  => $country

Updating a Record

In the table name user we have to update the username and password for the id =1.
require_once "./class/Db.php"; //include PHP database CLass
//insert in user table
    "username" => $username,
    "password" => $password,
     "country" => $country

Deleting a record

Suppose we have to delete the id = 1 from the table user.
require_once "./class/Db.php"; //include PHP database CLass
//delete a record

Fetching the record with single condition

Fretch all data from the table where country = India
require_once "./class/Db.php"; //include PHP database CLass
$r = Db::fetch("test",array("country" => "India"),array("="));
//display result

Fetching the record with multiple conditon

Suppose we have to fetch the data with the multiple condition look the below code.
require_once "./class/Db.php"; 
$result = Db::fetch("user",array(
    "email" => "email@gmail.com",
    "password" => "password"

Row Count

To get the number of rows from the table user follow the below code
require_once "./class/Db.php"; 
$row = Db::rowCount('test');

echo $row;

Close the connection

require_once "./class/Db.php"; //include PHP database CLass
//unset connecton

Please click the download link to get the DB.php file

By posted on - 17th Apr 2016

Social Oauth Login

Personalized Map Navigation

Online Image Compression Tool

Image Compression


Recent Posts