mysql封裝mysqli類

//conf.php
<?php
return array(
    'host'=>'localhost',
    'user'=>'root',
    'pwd'=>'910420',
    'db'=>'test',
    'charset'=>'utf8',
    'salt'=>'L&#7sd":Adfqef]',
);
?>

//Mysql.class.php
<?php
abstract class aDB {
    /**
     * 連接數據庫,從配置文件中讀取配置信息
     */
    abstract public function conn();
    /**
     * 發送query查詢
     * @param string $sql sql??
     * @return mixed
     */
    abstract public function query($sql);
    /**
     * 查詢多行數據
     * @param string $sql sql語句
     * @return array
     */
    abstract public function getAll($sql);
    /**
    * 單行數據
     * @param string $sql sql語句
     * @return array
     */
    abstract public function getRow($sql);
    /**
     * 查詢單個數據  count(*)
     * @param string $sql sql語句
     * @return mixed
     */
    abstract public function getOne($sql);
    /**
     * 自動創建sql并執行,insert/update
     * @param array $data  關聯數組 鍵/值 與表的 列/值對應
     * @param string $table 表明
     * @param string $act 動作/update/insert
     * @param string $where 條件,用于update
     * @return int 新插入的行的主鍵值或者影響行數
     */
    abstract public function Exec($data , $table , $act='insert' , $where='0');
    /**
     * 返回上一條insert語句產生的值
     */
    abstract public function lastId();
    /**
     * 返回上一條語句影響的行數
     */
    abstract public function affectRows();
}
class Mysql extends aDB{
    public $mysqli;
    public function __construct(){
        $this->conn();
    }
    /**
     * 連接數據庫,從配置文件中讀取配置信息
     */
    public function conn(){
        $conf=include './conf.php';
        $this->mysqli = new mysqli($conf['host'], $conf['user'], $conf['pwd'], $conf['db']);
        //設置字符集
        $this->mysqli->query('set names '.$conf['charset']);
    }
    /**
     * 發送query查詢
     * @param string $sql sql??
     * @return mixed
     */
    public function query($sql){
        //select返回一個對象或者無結果集sql返回bool
        return $this->mysqli->query($sql);
    }
    /**
     * 查詢多行數據
     * @param string $sql sql語句
     * @return array
     */
    public function getAll($sql){
        $data=[];
        $res=$this->mysqli->query($sql);
        while($row=$res->fetch_assoc()){
            $data[]=$row;
        }
        return $data;
    }
    /**
    * 單行數據
     * @param string $sql sql語句
     * @return array
     */
    public function getRow($sql){
        $res=$this->mysqli->query($sql);
        return $res->fetch_assoc();
    }
    /**
     * 查詢單個數據  count(*)
     * @param string $sql sql語句
     * @return mixed
     */
    public function getOne($sql){
        $res=$this->mysqli->query($sql);
        return $res->fetch_row()[0];
    }
    /**
     * 自動創建sql并執行,insert/update
     * @param array $data  關聯數組 鍵/值 與表的 列/值對應
     * @param string $table 表明
     * @param string $act 動作/update/insert
     * @param string $where 條件,用于update
     * @return int 新插入的行的主鍵值或者影響行數
     */
    public function Exec($data , $table , $act='insert' , $where='0'){
        if($act == 'insert'){
            $sql ="insert into $table (";
            $sql.=implode(',',array_keys($data)).") values('";
            $sql.=implode(array_values($data),"','")."')";          
        }else{
            $sql="update $table set ";
            foreach($data as $k => $v){
                $sql.=$k."='".$v."',";
            }
            $sql=rtrim($sql,',')." where ".$where;
        }
        return $this->mysqli->query($sql);
    }
    /**
     * 返回上一條insert語句產生的值
     */
    public function lastId(){
        return $this->mysqli->insert_id;
    }
    /**
     * 返回上一條語句影響的行數
     */
    public function affectRows(){
        return $this->mysqli->affected_rows;
    }
}
$a= new Mysql();
$b=$a->Exec(array('name'=>'zhao','age'=>15),'test');
var_dump($a->affectRows());//int(1)
var_dump($a->lastId());//int(7) 
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容