php封裝db 類連線sqlite3

staysober發表於2019-02-16
<?php
    class dbManager{
        public $db;
        function __construct(){
            if(!file_exists(`./db.php`)){
                $this->init();
                return;
            }
            $this->db = new SQLite3(`./db.php`);
        }
        function init(){
            $this->db = new SQLite3(`./db.php`);
            // TODO:
        }

        function changes(){
            return $this->db->changes();
        }

        function query($sql,$param=null,$memb=null){
            $stmt=$this->db->prepare($sql);
            if(!$stmt)
                return false;
            if($param){
                if(is_array($param)){
                    for($i=0;$i<count($param);$i++)
                        $stmt->bindValue($i+1,$param[$i]);
                }else{
                    $stmt->bindValue(1,$param);
                }
            }
            $rs=$stmt->execute();
            if(!$rs){
                $stmt->close();
                return false;
            }
            $arr=$rs->fetchArray(SQLITE3_NUM);
            $rs->finalize();
            $stmt->close();
            if(!$arr)
                return null;
            if(!$memb)
                return $arr;
            $res=array();
            for($i=0;$i<count($memb);$i++){
                $res[$memb[$i]]=$arr[$i];
            }
            return $res;
        }

        function queryAll($sql,$param=null,$memb=null){
            $stmt=$this->db->prepare($sql);
            if(!$stmt)
                return false;
            if($param){
                if(is_array($param)){
                    for($i=0;$i<count($param);$i++)
                        $stmt->bindValue($i+1,$param[$i]);
                }else{
                    $stmt->bindValue(1,$param);
                }
            }
            $rs=$stmt->execute();
            if(!$rs){
                $stmt->close();
                return false;
            }
            
            $res=array();
            while($arr=$rs->fetchArray(SQLITE3_NUM)){
                if(!$memb) {
                    $res[]=$arr;
                    continue;
                }
                if(count($memb)==1 && $memb[0]==null){
                    $res[]=$arr[0];
                    continue;
                }
                $it=array();
                for($i=0;$i<count($memb);$i++){
                    $it[$memb[$i]]=$arr[$i];
                }
                $res[]=$it;
            }
            $rs->finalize();
            $stmt->close();
            
            return $res;
        }

        function querySingle($sql,$param=null){
            $res=$this->query($sql,$param);
            if(!$res)
                return false;
            return $res[0];
        }
        
        function querySingleAll($sql,$param=null){
            $stmt=$this->db->prepare($sql);
            if(!$stmt)
                return false;
            if($param){
                if(is_array($param)){
                    for($i=0;$i<count($param);$i++)
                        $stmt->bindValue($i+1,$param[$i]);
                }else{
                    $stmt->bindValue(1,$param);
                }
            }
            $rs=$stmt->execute();
            if(!$rs){
                $stmt->close();
                return false;
            }
            
            $res=array();
            while($arr=$rs->fetchArray(SQLITE3_NUM)){
                $res[]=$arr[0];
            }
            $rs->finalize();
            $stmt->close();
            
            return $res;
        }

        function exec($sql,$param=null){
            $stmt=$this->db->prepare($sql);
            if(!$stmt)
                return false;
            if($param){
                if(is_array($param)){
                    for($i=0;$i<count($param);$i++)
                        $stmt->bindValue($i+1,$param[$i]);
                }else{
                    $stmt->bindValue(1,$param);
                }
            }
            $rs=$stmt->execute();
            if($rs) {
                $res=true;
                $rs->finalize();
            }else{
                $res=false;
            }
            $stmt->close();
            return $res;
        }
        
        function begin(){
            return $this->exec(`BEGIN`);
        }
        function rollback(){
            return $this->exec(`ROLLBACK`);
        }
        function commit(){
            return $this->exec(`COMMIT`);
        }
        
        function escapeString($s){
            return $this->db->escapeString($s);
        }
        //最新插入的id
        function lastInsertRowID(){
            return $this->db->lastInsertRowID();
        }
        
        function lastErrorMsg (){
            return $this->db->lastErrorMsg();
        }
    }
?>

PDO支援資料庫移植,如果你的部署將來有多種資料庫,那就用它了.同時,PDO是C設計的,執行效率較高.他已經封裝為PHP的擴充套件庫元件了.執行快,效率高

class dbManager{
    
    function __construct($config) {
        $text = file_get_contents($config);
        $obj = json_decode($text);
        $this->db= new PDO($obj -> dsn, $obj -> user, $obj -> pass);
    }
    
    private function bind_param($stmt,&$param){
        if ($param || $param===0) {
            if (is_array($param)) {
                for ($i = 0; $i < count($param); $i++)
                    $stmt -> bindParam($i + 1, $param[$i]);
            } else {
                $stmt -> bindParam(1, $param);
            }
        }
    }
    
    function exec($sql, $param = null) {
        $stmt = $this -> db -> prepare($sql);
        if (!$stmt){
            var_dump($this->db->errorinfo());
            return false;
        }
        $this->bind_param($stmt,$param);
        $res = $stmt -> execute();
        if($res){
            $res=$stmt->rowCount();
        }
        $stmt -> closeCursor();
        return $res;
    }
    
    function query($sql,$param=null,$name=true){
        $stmt = $this -> db -> prepare($sql);
        if (!$stmt){
            return null;
        }
        $this->bind_param($stmt,$param);
        $res = $stmt -> execute();
        if(!$res){
            $stmt->closeCursor();
            return null;
        }
        $arr=$stmt->fetch($name?PDO::FETCH_ASSOC:PDO::FETCH_NUM);
        $stmt -> closeCursor();
        return $arr;
    }
    
    function queryAll($sql,$param=null,$name=true){
        $stmt = $this -> db -> prepare($sql);
        if (!$stmt){
            return null;
        }
        $this->bind_param($stmt,$param);
        $res = $stmt -> execute();
        if(!$res){
            $stmt->closeCursor();
            return null;
        }
        $arr=$stmt->fetchAll($name?PDO::FETCH_ASSOC:PDO::FETCH_NUM);
        $stmt -> closeCursor();
        return $arr;
    }
    
    function querySingle($sql,$param=null,$name=true){
        $stmt = $this -> db -> prepare($sql);
        if (!$stmt){
            return null;
        }
        $this->bind_param($stmt,$param);
        $res = $stmt -> execute();
        if(!$res){
            $stmt->closeCursor();
            return null;
        }
        $arr=$stmt->fetchColumn();
        $stmt -> closeCursor();
        return $arr;
    }
    
    function querySingleAll($sql,$param=null,$name=true){
        $stmt = $this -> db -> prepare($sql);
        if (!$stmt){
            return null;
        }
        $this->bind_param($stmt,$param);
        $res = $stmt -> execute();
        if(!$res){
            $stmt->closeCursor();
            return null;
        }
        $arr=[];
        do{
            $item=$stmt->fetchColumn();
            if($item)
                $arr[]=$item;
        }while($item!==false);
        $stmt -> closeCursor();
        return $arr;
    }

    public function begin() {
        $this -> db -> beginTransaction();
    }

    public function rollback() {
        $this -> db -> rollBack();
    }

    public function commit() {
        $this -> db -> commit();
    }
}

/*
$db=new dbManager("db.json");
$db->exec(`CREATE TABLE USER(ID INTEGER PRIMARY KEY NOT NULL)`);
$db->exec(`INSERT INTO user(ID) VALUES(?)`,1);
var_dump($db->query(`SELECT * FROM USER WHERE ID=?`,1));
var_dump($db->queryAll(`SELECT * FROM USER WHERE ID=?`,1));
var_dump($db->querySingle(`SELECT * FROM USER WHERE ID=?`,1));
var_dump($db->querySingleAll(`SELECT * FROM USER WHERE ID=?`,1));
*/

?>

這是修改為pdo版本的原生db類
匯入的json配置檔案
{

"dsn":"sqlite:test.db",
"user":"",
"pass":""

}
我這裡只是方便前端修改,也可以搞成php檔案

相關文章