標籤:
class sqlHelper{ private $conn; private $host = ‘localhost‘; private $user = ‘root‘; private $pwd = ‘root‘; private $db; public function __construct($idb){ $this->db = $idb; $this->conn = new mysqli($this->host,$this->user,$this->$this->pwd,$this->idb); if($this->conn->connect_error){ $this->error($this->conn->connect_error); } $this->conn->query(‘set names utf8‘); } //針對select語句 public function execute_dql($sql){ $res = $this->conn->query($sql) or $this->error(‘查詢操作失敗‘.$this->conn->error); $this->free(); return $res; }//針對insert、update、delectpublic function execute_dml($sql){ $flag = $this->conn->query($sql) or $this->error($this->conn->error); if(!$flag){ return 0 ;//操作失敗 }else{ if($this->conn->afffected_rows > 0){ return 1;//操作成功 }else{ return 2;//操作失敗沒有行數受到影響 } }}//針對多行記錄public function fetch_all($sql){ $res = $this->conn->query($sql) or $this->error(‘查詢操作失敗‘.$this->conn->error); $arr = array(); while($row = $res->fetch_assoc()){ $arr[] = $row; //及時釋放資源 } return $arr;}protected function error($err){ $log = ‘cur.log‘; file_put_contens($log,$err,FILE_APPEND); die($err);}}
php-基於物件導向的MySQL類