PHP數(shù)據(jù)庫(kù)類

<?php

class Db{

    //私有靜態(tài)屬性存儲(chǔ)實(shí)例化對(duì)象自身
    private static $instance;

    //存儲(chǔ)PDO類的實(shí)例化
    private $pdo;

    //PDOStatement類
    private $stmt;

    //禁止外部實(shí)例化對(duì)象,鏈接數(shù)據(jù)庫(kù)
    private function __construct($config,$port,$charset){
        try{
            $this->pdo = new PDO('MySQL:host='.$config['host'].';dbname='.$config['dbname'].';charset='.$charset.';port='.$port.'',$config['user'],$config['password']);
            //開(kāi)始事務(wù)回滾的錯(cuò)誤警告
            $this->pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
        }catch(PDOException $e){
            echo 'msyql error: '.$e->getMessage();
        }

    }

    //外部實(shí)例化
    public static function getInstance($config,$port=3306,$charset='utf8'){
        if(!self::$instance instanceof Db){
            self::$instance = new self($config,$port,$charset);
        }

        return self::$instance;
    }

    //插入數(shù)據(jù)
    public function insertData($table,$data){
        //拼接插入的數(shù)據(jù)庫(kù)字段名
        $columns =  implode(',',array_keys($data));
        //憑借插入的數(shù)據(jù)
        $values = ':'.implode(',:',array_keys($data));
        $sql = "insert into $table ($columns) values ($values)";

        //處理date數(shù)據(jù)格式
        $newdata = $this->doData($data);

        //預(yù)處理sql,執(zhí)行添加數(shù)據(jù)操作
        $this->doSql($sql,$newdata);
        //成功執(zhí)行后返回插入數(shù)據(jù)的id,失敗返回錯(cuò)誤信息
        if($this->stmt->errorCode()==00000){
            return $this->pdo->lastInsertId();
        }else{
            return $this->stmt->errorInfo()[2];
        }
    }

    //更新數(shù)據(jù)
    public function updateData($table,$data,$where){

        //檢查需要更新的數(shù)據(jù)是否存在
        if(!$this->getOne($table,array_keys($data)[0],$where)){
            echo '需要更新的數(shù)據(jù)不存在';
            return false;
        }

        //檢查傳入的數(shù)據(jù)并且拼接成字符串
        if(is_array($data)){
            $columns = '';
            foreach($data as $k=>$v){
                $columns.= $k.'=:'.$k.',';
            }
            $columns=rtrim($columns,',');
        }

        //sql語(yǔ)句
        $sql = "UPDATE $table SET $columns WHERE ".$where;

        //傳入的數(shù)組處理成pdo定的數(shù)據(jù)格式[':user_name'=>'lisi',':user_age'=>39]
        $newdata = $this->doData($data);
        //預(yù)處理sql,執(zhí)行添加數(shù)據(jù)操作
        $this->doSql($sql,$newdata);
        //沒(méi)有錯(cuò)誤返回1,有錯(cuò)誤返回錯(cuò)誤信息
        if($this->stmt->errorCode()==00000){
            return 1;
        }else{
            return $this->stmt->errorInfo()[2];
        }
    }

    //查詢一條數(shù)據(jù)
    public function getOne($table,$fields,$where){
        //檢查傳入的字段是否是數(shù)組
        if(is_array($fields)){
            if(count($fields)>1){
                $columns=implode(',',$fields);
            }else{
                $columns=$fields[0];
            }   
        }else{
            $columns = $fields;
        }

        //查詢語(yǔ)句
        $sql = "SELECT $columns FROM $table WHERE ".$where.' LIMIT 1';
        //預(yù)處理sql,執(zhí)行添加數(shù)據(jù)操作
        $this->doSql($sql);
        //獲得查詢的結(jié)果集
        $res = $this->stmt->fetch(PDO::FETCH_ASSOC);
        //如果沒(méi)有錯(cuò)誤則返回查詢的結(jié)果集,否則返回錯(cuò)誤信息
        if($this->stmt->errorCode()==00000){
            return $res;
        }else{
            return $this->stmt->errorInfo()[2];
        }   
    }

    //查詢多條數(shù)據(jù)
    public function getAll($table,$fields,$where=1){
        //檢查傳入的字段是否是數(shù)組
        if(is_array($fields)){
            if(count($fields)>1){
                $columns=implode(',',$fields);
            }else{
                $columns=$fields[0];
            }   
        }else{
            $columns = $fields;
        }

        //查詢語(yǔ)句
        $sql = "SELECT $columns FROM $table WHERE ".$where;
        //預(yù)處理sql,執(zhí)行添加數(shù)據(jù)操作
        $this->doSql($sql);
        //獲得查詢的結(jié)果集
        $res = $this->stmt->fetchALL(PDO::FETCH_ASSOC);
        //如果沒(méi)有錯(cuò)誤則返回查詢的結(jié)果集,否則返回錯(cuò)誤信息
        if($this->stmt->errorCode()==00000){
            return $res;
        }else{
            return $this->stmt->errorInfo()[2];
        }   
    }

    //刪除數(shù)據(jù)
    public function delData($table,$where){
        //檢查需要更新的數(shù)據(jù)是否存在
        if(!$this->getOne($table,['*'],$where)){
            echo '需要?jiǎng)h除的數(shù)據(jù)不存在';
            return false;
        }
        if(!$where){
            echo '請(qǐng)?zhí)砑觿h除條件';
            return false;
        }
        $sql = "DELETE FROM $table WHERE ".$where;
        //預(yù)處理sql,執(zhí)行添加數(shù)據(jù)操作
        $this->doSql($sql);
        //如果沒(méi)有錯(cuò)誤則返回查詢的結(jié)果集,否則返回錯(cuò)誤信息
        if($this->stmt->errorCode()==00000){
            return 1;
        }else{
            return $this->stmt->errorInfo()[2];
        }
    }

    //處理傳入的數(shù)據(jù)
    private function doData($data){
        foreach($data as $k=>$v){
            $newk = ':'.$k;
            $newdata[$newk]=$v;
        }
        return $newdata;
    }

    //執(zhí)行sql操作
    function doSql($sql,$data=null){
        //預(yù)處理sql
        $this->stmt=$this->pdo->prepare($sql);
        //執(zhí)行添加數(shù)據(jù)操作
        $this->stmt->execute($data);
    }

    //禁止外部克隆
    private function __clone(){}

}
$config = ['host'=>'127.0.0.1','dbname'=>'shop','user'=>'root','password'=>'root'];
$db = Db::getInstance($config,3306,'utf8');

//插入數(shù)據(jù)
$table= 'user';
$data=['user_name'=>'sunliang','user_age'=>28,'sex'=>1,'email'=>'323424958@126.com','password'=>md5(123456),'money'=>544545.34];
echo $db->insertData($table,$data);
// $sql = 'insert into (username,password) values (:username,password)';
// $stmt = $db->prepare($sql);
// $data = [':username'=>'lisi',':password'=>123456];
// $stmt->execute($data);

//修改數(shù)據(jù)
// $table = 'user';
// $data=['user_name'=>'sunliang','user_age'=>28,'email'=>'323424958@qq.com'];
// $sql = "update user set user_name=:username,user_age=:user_age,email=:email where id = 1";
// echo $db->updateData($table,$data,'id=18');
// $stmt = $db->prepare($sql);
// $stmt->execute($data);

//查詢一條數(shù)據(jù)
// $table = 'user';
// $fields= ['user_name','id'];
// $sql = 'select user_name,email from user where id = 1';
// print_r($db->getOne($table,$fields,'id=2'));

//查詢多條數(shù)據(jù)
// $table = 'user';
// $fields= ['user_name','id'];
// $sql = 'select user_name,email from user where id = 1';
// print_r($db->getAll($table,$fields));

//刪除數(shù)據(jù)
// $table = 'user';
// echo $db->delData($table,'id=16');

簡(jiǎn)單的數(shù)據(jù)庫(kù)類

成都創(chuàng)新互聯(lián)公司專注骨干網(wǎng)絡(luò)服務(wù)器租用十載,服務(wù)更有保障!服務(wù)器租用,西信服務(wù)器托管 成都服務(wù)器租用,成都服務(wù)器托管,骨干網(wǎng)絡(luò)帶寬,享受低延遲,高速訪問(wèn)。靈活、實(shí)現(xiàn)低成本的共享或公網(wǎng)數(shù)據(jù)中心高速帶寬的專屬高性能服務(wù)器。

網(wǎng)站名稱:PHP數(shù)據(jù)庫(kù)類
本文路徑:http://bm7419.com/article22/gegscc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷型網(wǎng)站建設(shè)、虛擬主機(jī)、網(wǎng)站策劃、電子商務(wù)、、動(dòng)態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作