php封装db|php封装db 类连接sqlite3

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; $ibindValue($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; $idb->prepare($sql); if(!$stmt) return false; if($param){ if(is_array($param)){ for($i=0; $ibindValue($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; $ifinalize(); $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; $ibindValue($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; $ibindValue($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封装db|php封装db 类连接sqlite3】}
我这里只是方便前端修改,也可以搞成php文件

    推荐阅读