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
    try{
        $con = new PDO("mysql:host=localhost;dbname=test","root","password");
    }catch (PDOException $e){
        die($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
    $stmt->execute();
    //fetch the result
    $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
    //display the result
    print "<pre>";
    print_r($result);
    print "</pre>";

Fetch data in PHP Database Class

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

Getting Started With PHP Database Class

Configure

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

<?php
    require_once "./class/Db.php"; //include PHP database CLass
    Db::getConnection();
?>

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
    }else{
       //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
    Db::insert("user",array(
        "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  
    Db::update("test",array(
        "username" => "newusername",
        "password" => "newpassword"),
        array("id","=",1));

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
Db::delete("test",array("id","=",1));



Fetching a record with single condition

Fetch all from test table where password = password

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

fetching a record with multiple condition

require_once "core/init.php";

$result = Db::fetch("user",array(
    "email" => "email@gmail.com",
    "password" => "password"
),array("=","!="));

print_r($result);

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),
    array("=","!="));

echo $row;

Unset Connection

Unset Connection

require_once "./class/Db.php"; //include PHP database CLass
//unset connecton
Db::unsetConnection();



2 thoughts on “PHP Database Class in PDO”

Leave a Reply

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