php database class

PHP Database Class in PDO

PHP Database class is a kickass class build to handle all the Database functionality. PHP database class make it super easy to handle CRUD(Create Read Update Delete) operations easily, PHP database class is build using PDO and BindParams so its SQL injection safe, IN my all projects i use PHP database class so i hope it will help you guyz build web application faster and safer. SO get Started

Whats the need to PHP Database Class

PHP Database class prevent you from writing more code, You may have to write 10 lines of code to execute a simple fetch query in PDO but by using PHP Database Class you can execute a fetch query in 1 line

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 test 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 "<pre>";
    print "</pre>";

Fetch data in PHP Database Class

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

Getting Started With PHP Database Class


First thing first you have to configure the PHP Database Class by configure i mean you have to provide your host, database name, username, password

  1. Go to Db.php(PHP database class)
  2. Edit $_host,$_user,$_pass,$_db and put your hostname,database,username,password

php database class

Start a connection

if connection is not started this method starts a new connection and return the connection object

    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 we have database name user we want to store username and password

    require_once "./class/Db.php"; //include PHP database CLass
    //insert in user table
        "username" => $username,
        "password" => $password

Updating a record

Suppose we have a table name test we want to change username to newusername and password to newpassword where id = 1

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

    //update query  
        "username" => "newusername",
        "password" => "newpassword"),

Deleting a record

Suppose we have a table name test we want to delete records where id = 1

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

//delete a record

Fetching a record with single condition

Fetch all from test table where password = password

require_once "./class/Db.php"; //include PHP database CLass
$r = Db::fetch("test",array("password" => "password"),array("="));
//display result

fetching a record with multiple condition

require_once "core/init.php";

$result = Db::fetch("user",array(
    "email" => "",
    "password" => "password"


Row count

get the number of rows from test table where username = husain and id != 1

require_once "core/init.php";

$row = Db::rowCount('test',array(
    "username" => "husain",
    "id" => 2),

echo $row;

Unset Connection

Unset Connection

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

2 thoughts on “PHP Database Class in PDO

Leave a Reply

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