Create Class for database operations using pdo

Hi guys please find the new connection and db operations class using pdo in php technology

<?php
Class Db
{
    private $strHost = 'localhost';
    private $strDB = 'classgod';
    private $strUser = 'root';
    private $strPass = '';
    public $strSQL = '';
    public $intTotalRows = '';

    public function __construct($strHost = '', $strDB = '', $strUser = '', $strPass = '')
    {
        try
        {
            if ($strHost != '')
            {
                $this->strHost = $strHost;
            }
            if ($strDB != '')
            {
                $this->strDB = $strDB;
            }
            if ($strUser != '')
            {
                $this->strUser = $strUser;
            }
            if ($strPass != '')
            {
                $this->strPass = $strPass;
            }
            $this->objDB = new PDO("mysql:host=" . $this->strHost . ";dbname=" . $this->strDB.";charset=UTF8;", $this->strUser, $this->strPass, array(PDO::ATTR_PERSISTENT => false));
            //$this->objDB->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); //set error exception
            if ($this->objDB)
            {
                return $this->objDB;
            }
            else
            {
                echo "Database Connection Failed.";
                die;
            }
        } catch (Exception $objException)
        {
            echo $objException->getMessage();
            exit;
        }
    }

    public function select($strSql = '')
    {
        $this->strSQL = $strSql;
        try
        {
            if ($this->strSQL != '')
            {
                $objStmt = $this->objDB->prepare($this->strSQL);
                $objStmt->execute();
                $arrReturn = $objStmt->fetchAll(PDO::FETCH_ASSOC);
                $objStmt = $this->objDB->prepare("SELECT FOUND_ROWS() as totalcount");
                $objStmt->execute();
                $arrTemp = $objStmt->fetch();
                $this->intTotalRows = $arrTemp['totalcount'];
                return $arrReturn;
            }
        } catch (Exception $objException)
        {
            echo $objException->getMessage();
            exit;
        }
    }
    public function insert($strSql = '')
    {
        $this->strSQL = $strSql;
        try
        {
            if ($this->strSQL != '')
            {
                $objStmt = $this->objDB->prepare($this->strSQL);
                $objStmt->execute();
                return $this->objDB->lastInsertId();
            }
        }
        catch (Exception $objException)
        {
            echo $objException->getMessage();
            exit;
        }
    }
    public function updatedel($strSql = '')
    {
        $this->strSQL = $strSql;
        try
        {
            if ($this->strSQL != '')
            {
                $objStmt = $this->objDB->prepare($this->strSQL);
                return $objStmt->execute();
            }
        } catch (Exception $objException)
        {
            echo $objException->getMessage();
            exit;
        }
    }

}

$objData = new Db();

// example
$arrData = $objData->insert("INSERT INTO tbl_skills(sk_name,cat_id) values('testing','2')");
echo "<pre>";print_r($arrData);die;
?>
Advertisements