PHP 单例模式 封装链接mysql类

<?php
    /*
     *describe:单例模式 数据库类
    单例模式的必要条件
    (1)私有的构造方法-为了防止在类外使用new关键字实例化对象
    (2)私有的成员属性-为了防止在类外引入这个存放对象的属性
    (3)私有的克隆方法-为了防止在类外通过clone成生另一个对象
    (4)公有的静态方法-为了让用户进行实例化对象的操作
    */
    header("content-type:text/html;charset=utf-8");
    final class DB{
        private $charset = "utf8";         //字符串编码
        private $link = NULL;              //数据库连接
        private static $instance = NULL;   //存储对象

        //初始化
        private function __construct($hostfield,$userfield,$password,$datebase){
            $this->link = mysqli_connect($hostfield,$userfield,$password,$datebase);
            if(!$this->link){
                die("连接失败: " . mysqli_connect_error());
            }
            mysqli_query($this->link,"SET fieldS ".$this->charset);
            return $this->link;
        }

        //防止克隆
        private function __clone(){}

        //静态公共接口
        public static function getInstance($hostfield,$userfield,$password,$datebase){
            if(!self::$instance instanceof self){
                self::$instance = new DB($hostfield,$userfield,$password,$datebase);
            }
            return self::$instance;
        }

        /*
         *describe:查询单个字段
         *@param $field        type:string  传入需要筛选的字段
         *@param $table_name   type:string  需要查询的表名
         *@param $where        type:array   需要筛选的where条件
        */
        public function getField($field="",$table_name="",$where=""){
            $where_string = "";
            //where条件
            $where_string = $this->getWhere($where);
            if(is_string($field)){
                //组装sql
                $sql = "SELECT {$field} FROM {$table_name} WHERE {$where_string}";
                $result = mysqli_query($this->link,$sql);
                while($res = mysqli_fetch_array($result,MYSQL_ASSOC)){
                    $info[] = $res;
                }
                //若只有一个数组,则返回一维数组
                if(count($info)==1){
                    return $info[0];
                }else{
                    return $info;
                }
            }

        }

        /*
         *describe:查询单条语句
         *@param $sql 查询语句
         *@param $type 返回数组的类型
         *        MYSQL_ASSOC - 关联数组(默认)
         *        MYSQL_NUM - 数字数组
         *        MYSQL_BOTH -同时产生关联和数字数组
        */
        public function getRow($sql,$type=MYSQL_ASSOC){
            $result = mysqli_query($this->link,$sql);
            $res = mysqli_fetch_array($result,$type);
            return $res;
        }

        /*
         *describe:查询多条语句
         *@param $sql 查询语句
         *@param $type 返回数组的类型
         *        MYSQL_ASSOC - 关联数组(默认)
         *        MYSQL_NUM - 数字数组
         *        MYSQL_BOTH -同时产生关联和数字数组
        */
        public function getRows($sql,$type=MYSQL_ASSOC){
            $result = mysqli_query($this->link,$sql);
            while($rows = mysqli_fetch_array($result,$type)){
                $res[] = $rows;
            }
            return $res;
        }

        /*
         *describe:增加语句
         *@param type:array or string $info 传入的值  
                     若为数组则进行拼接SQL语句,并新增 返回新增的ID
                     若传入的为SQL语句则直接执行,返回新增的ID
         *@param $table_name  为需要新增的表名(可选)
        */
        public function add($info,$table_name = ""){
            //判断是否为数组,进行SQL拼接
            if(is_array($info)){
                $field = "";
                $val ="";
                $fields = "";
                $vals ="";
                foreach($info as $k=>$v){
                    $field .= "`".$k."`,";
                    $val .= "'".$v."',";
                }
                //$fields =  substr($field,0,strlen($field)-1);
                //$vals = substr($val,0,strlen($val)-1);
                $fields = trim($field,",");
                $vals = trim($val,",");
                $sql = "INSERT INTO {$table_name} ({$fields}) VALUES ({$vals})";
                if(mysqli_query($this->link,$sql)){
                    return mysqli_insert_id($this->link);
                }else{
                    echo "error:".$sql."<br/>".mysqli_error($this->link);
                }
            //判断是否为SQL语句
            }else if(is_string($info)){
                if(mysqli_query($this->link,$info)){
                    return mysqli_insert_id($this->link);
                }else{
                    echo "error:".$info."<br/>".mysqli_error($this->link);
                }
            }else{
                echo "error";
            }
        }

        /*
         *describe:更新
         *@param type:array            $info       传入的修改的数组
         *@param type:string           $table_name 需修改的表名
         *@param type:array            $where      修改的where条件,传入数组
        */
        public function update($info,$table_name = "",$where=""){
            //判断是否是数组,进行SQL拼接
            if(is_array($info)){
                $field = "";
                $fields = "";
                //拼接修改语句
                foreach($info as $k=>$v){
                    $field .= "`{$k}`='{$v}',";
                }
                $fields = trim($field,",");
                //拼接where条件
                $where_string = "";            
                $where_string = $this->getWhere($where);
                //拼接SQL
                $sql = "UPDATE {$table_name} SET {$fields} where {$where_string}";
                echo $sql;
                die();
                if(mysqli_query($this->link,$sql)){
                    return mysqli_affected_rows($this->link);
                }else{
                    echo "error:".$sql."<br/>".mysqli_error($this->link);
                }
            }else{
                echo "error";
            }
        }

        /*
         *describe:删除
         *@param $where        type:array   传入需要删除的where条件  为数组
         *@param $table_name   type:string  需要删除的表名
        */
        public function del($where,$table_name=""){
            if(is_array($where)){
                $where_string ="";
                //拼接where条件
                $where_string = $this->getWhere($where);
                $sql = "DELETE FROM {$table_name} WHERE {$where_string}";
                if(mysqli_query($this->link,$sql)){
                    return mysqli_affected_rows($this->link);
                }else{
                    echo "error:".$sql."<br/>".mysqli_error($this->link);
                }
            }else{
                echo "error";
            }
        }

        /*
         *describe:执行原生sql
         *@param $sql  type:string 需要执行的sql
        */
        public function query($sql){
            if(mysqli_query($this->link,$sql)){
                return mysqli_affected_rows($this->link);
            }else{
                echo "error:".$sql."<br/>".mysqli_error($this->link);
            }
        }

        /*
         *describe:拼接where条件
         *@param $where type:array  传入where为数组的条件
         */
         private function getWhere($where){
            $where_string = "";
            if(is_array($where)){
                foreach($where as $k=>$v){
                    if(is_array($v)){
                        foreach($v as $kk=>$vv){
                            $where_string .= "`{$k}` {$kk} '{$vv}' AND ";
                        }
                    }else{
                        $where_string .= "`{$k}`='{$v}' AND ";
                    }
                }
            }
            return rtrim($where_string,"AND ");
         }
    }


    //测试 mysql类
    $class = DB::getInstance("localhost","root","123456","test");
    $data = array(
        "city_name"=>"test12222222",
        "pid"=>"111111",
    );
    $where = array(
        "id"=>"438",
        "city_name"=>array("like"=>"%test%"),
    );
    //var_dump($class->query("DELETE FROM city WHERE `city_name` LIKE '%test%'"));
    //var_dump($class->add($data,"city"));
    //var_dump($class->del($where,"city"));
    //var_dump($class->update($data,"city",$where));
    //var_dump($class->getField("city_name,pid","city",$where));

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

微信扫一扫

微信扫一扫

微信扫一扫,分享到朋友圈

PHP  单例模式   封装链接mysql类