自定義DB類 --- 2021-09-06

自定義mysql DB類 實現(xiàn)增刪該查

<?php 
//數(shù)據(jù)庫連接類
class DB{
    //私有屬性
    private static $dbcon = false;
    private $host;
    private $port;
    private $user;
    private $pass;
    private $db;
    private $charset;
    private $link;

    //私有構(gòu)造方法
    private function __construct(){
        $this->host = 'rm-uf6kvqj8697g106ccmo.mysql.rds.aliyuncs.com';
        $this->port = '3306';
        $this->user = 'weiyuntao';
        $this->pass = 'Wyt123456';
        $this->db = 'queue';
        $this->charset = 'utf8';
        //連接數(shù)據(jù)庫
        $this->db_connect();
        //選擇數(shù)據(jù)庫
        $this->db_userdb();
        //設(shè)置字符集
        $this->db_charset();
    }

    //連接數(shù)據(jù)庫
    private function db_connect(){
        $this->link = mysqli_connect(
            $this->host,
            $this->user,
            $this->pass,
            $this->db,
            $this->port
            );
        if(!$this->link){
            echo "連接數(shù)據(jù)庫失敗<br>";
            echo "錯誤編碼".mysqli_errno($this->link)."<br>";
            echo "錯誤編碼".mysqli_error($this->link)."<br>";
            exit;
        }
    }

    //設(shè)置字符集
    private function db_charset(){
        mysqli_query($this->link,"set names {$this->charset}");
    }

    //選擇數(shù)據(jù)庫
    private function db_userdb(){
        $result = mysqli_query($this->link,"use {$this->db}");
    }

    //私有的克隆
    private function __clone(){
        die('clone is not allowed');
    }

    //公用的靜態(tài)方法 $db = DB::getIntance();
    public static function getIntance(){
        if(self::$dbcon==false){
            self::$dbcon=new self;
        }
        return self::$dbcon;
    }

    //執(zhí)行sql語句
    public function query($sql){
        $res = mysqli_query($this->link,$sql);
        if(!$res){
            echo "sql語句執(zhí)行失敗<br>";
            echo "錯誤編碼是".mysqli_errno($this->link)."<br>";
            echo "錯誤信息是".mysqli_error($this->link)."<br>";  
        }
        return $res;
    }

    //獲取最后一條記錄id
    public function getInsertid(){
        return mysqli_insert_id($this->link);
    }

    /**
    *查詢某個字段
    *@param
    *@return string or int
    */
    public function getOne($sql){
        $query = $this->query($sql);
        return mysqli_free_result($query);
    }

    //獲取一行記錄,return array一維數(shù)組
    public function getRaw($sql,$type="assoc"){
        $query = $this->query($sql);
        if(!in_array($type,array("assoc","array","row"))){
            die("mysqli_query error");
        }
        $funcname = "mysqli_fetch_".$type;
        return $funcname($query);
    }

    //獲取多條數(shù)據(jù)碧囊,二維數(shù)組
    public function getAll(){
        $query = $this->query($sql);
        $list = array();
        while($r = $this->getFormSource($query)){
            $list[]=$r;
        }
        return $list;
    }

    public function selectAll($table,$where,$fields = '*',$order='',$skip=0,$limit=1000){
        if(is_array($where)){
            foreach ($where as $key => $val) {
                if(is_numeric($val)){
                    $condition = $key.'='.$val;
                }else{
                    $condition = $key.'=\".$val.\"';
                }
            }
        }else{
            $condition = $where;
        }   
        if(!empty($order)){
            $order = "order by ".$order;
        }
        $sql = "select $fields from $table where $condition $order limit $skip,$limit";
        $result = $this->query($sql);
        if($result){
            return true;
        }else{
            return false;
        }
    }   

    /**
    *添加數(shù)據(jù)
    *@param string $table 表名
    *@param string or array $data[數(shù)據(jù)]
    *@return int 最新添加的id
    */
    public function insert($table,$data){
        $key_str = '';
        $v_str = '';
        foreach ($data as $key => $v) {
            $key_str.=$key.',';
            $v_str.= "'$v',";
        }
        $key_str = trim($key_str,',');
        $v_str = trim($v_str,',');
        //判斷數(shù)據(jù)是否為空
        $sql = "insert into $table ($key_str) values ($v_str)";
        $this->query($sql);
        return $this->getInsertid();
    }

    //刪除一條數(shù)據(jù)
    /**
    *@param $table $where=array('id'=>'a') 表名 條件
    *@return 受影響的行數(shù)
    */
    public function deleteOne($table,$where){
        if(is_array($where)){
            foreach ($where as $key => $val) {
                $condition = $key.'='."'$val'";     
            }
        }else{
            $condition = $where;
        }
        $sql = "delete from $table where $condition";
        $this->query($sql);
        //返回受影響的行數(shù)
        return mysqli_affected_rows($this->link);
    }

    /**
    *刪除多條數(shù)據(jù)方法
    *@param1 $table $where 表名 條件
    *@reurn 受影響的行數(shù)
    */
    public function deleteAll($table,$where){
        if(is_array($where)){
            foreach ($where as $key => $val) {
                if(is_array($val)){
                    $condition = $key.' '.'in('.implode(',',$val).')';
                }else{
                    $condition = $key.'='.$val;
                }
            }
        }else{
            $condition = $where;
        }
        $sql = "delete from $table where $condition";
        $this->query($sql);

        //返回受影響的行數(shù)
        return mysqli_affected_rows($this->link);
    }

    /**
    *修改操作
    *@param $table $data $where 表名 數(shù)據(jù) 條件
    */

    public function update($table,$data,$where,$limit = 0){
        //遍歷數(shù)組栏妖,得到每一個字段和字段的值
        $str = '';
        foreach ($data as $key => $v) {
            $str .= "$key='$v',";
        }
        $str = trim($str,',');
        if(is_array($where)){
            foreach ($where as $key => $val) {
                if(is_array($val)){
                    $condition = $key.'in ('.implode(',',$val).')';
                }else{
                    $condition = $key.'='.$val;
                }
            }
        }else{
            $condition = $where;
        }

        if(!empty($limit)){
            $limit = "limit".' '.$limit;
        }else{
            $limit = '';
        }
        //修改sql語句
        $sql = "update $table set $str where $condition $limit";
        $res = $this->query($sql);
        if($res){
            return true;
        }else{
            return false;
        }
        
    }



}



?>

引用使用:

<?php 
    include './db.php';
    $db = DB::getIntance();

    $data = [
        'mobile'=>'13525728794'
    ];
    $res = $db->insert('order_queue',$data);
    var_dump($res);
?>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末挺峡,一起剝皮案震驚了整個濱河市荣瑟,隨后出現(xiàn)的幾起案子世剖,更是在濱河造成了極大的恐慌杆怕,老刑警劉巖间聊,帶你破解...
    沈念sama閱讀 217,509評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件攒盈,死亡現(xiàn)場離奇詭異,居然都是意外死亡甸饱,警方通過查閱死者的電腦和手機沦童,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評論 3 394
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來叹话,“玉大人偷遗,你說我怎么就攤上這事⊥蘸” “怎么了氏豌?”我有些...
    開封第一講書人閱讀 163,875評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長热凹。 經(jīng)常有香客問我泵喘,道長,這世上最難降的妖魔是什么般妙? 我笑而不...
    開封第一講書人閱讀 58,441評論 1 293
  • 正文 為了忘掉前任纪铺,我火速辦了婚禮,結(jié)果婚禮上碟渺,老公的妹妹穿的比我還像新娘鲜锚。我一直安慰自己,他們只是感情好苫拍,可當我...
    茶點故事閱讀 67,488評論 6 392
  • 文/花漫 我一把揭開白布芜繁。 她就那樣靜靜地躺著,像睡著了一般绒极。 火紅的嫁衣襯著肌膚如雪骏令。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,365評論 1 302
  • 那天垄提,我揣著相機與錄音榔袋,去河邊找鬼周拐。 笑死,一個胖子當著我的面吹牛摘昌,可吹牛的內(nèi)容都是我干的速妖。 我是一名探鬼主播,決...
    沈念sama閱讀 40,190評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼聪黎,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了备恤?” 一聲冷哼從身側(cè)響起稿饰,我...
    開封第一講書人閱讀 39,062評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎露泊,沒想到半個月后喉镰,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,500評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡惭笑,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,706評論 3 335
  • 正文 我和宋清朗相戀三年侣姆,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片沉噩。...
    茶點故事閱讀 39,834評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡捺宗,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出川蒙,到底是詐尸還是另有隱情蚜厉,我是刑警寧澤,帶...
    沈念sama閱讀 35,559評論 5 345
  • 正文 年R本政府宣布畜眨,位于F島的核電站昼牛,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏康聂。R本人自食惡果不足惜贰健,卻給世界環(huán)境...
    茶點故事閱讀 41,167評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望恬汁。 院中可真熱鬧伶椿,春花似錦、人聲如沸蕊连。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽甘苍。三九已至尝蠕,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間载庭,已是汗流浹背看彼。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評論 1 269
  • 我被黑心中介騙來泰國打工廊佩, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人靖榕。 一個月前我還...
    沈念sama閱讀 47,958評論 2 370
  • 正文 我出身青樓标锄,卻偏偏與公主長得像,于是被迫代替她去往敵國和親茁计。 傳聞我的和親對象是個殘疾皇子料皇,可洞房花燭夜當晚...
    茶點故事閱讀 44,779評論 2 354

推薦閱讀更多精彩內(nèi)容