单例模式写MySQL model类,简单的增、删、改、查

单例模式的用途,可用于数据库操作

<?php

Class Db
{
    static private  $whe;//条件
    static private $tab;//表名
    static private $lim;//分段变量
    static private $order_by;//排序
    private $str_key = array();//添加数据的键
    private $str_val = array();//添加数据的值
    private $key_val = array();//修改的数据
    //防止外部实例对象
    private function __construct(){}
    //防止克隆
    private function __clone(){}

    //唯一一个实例对象
    static public $new_obj;
    static public function table($t)
    {
        if (!self::$new_obj) {
            self::$new_obj = new Db;
        }
        self::$tab=$t;
        return self::$new_obj;
    }
    //链接数据库
    public function mysql_connect()
    {
        $mysql_conf = array(
            'host'=>'127.0.0.1:3306',//连接地址
            'db'=>'test',//库名
            'db_user'=>'root',//账号
            'db_pwd'=>'root'//密码
        );
        return new MySQLi($mysql_conf['host'] , $mysql_conf['db_user'], $mysql_conf['db_pwd'] ,$mysql_conf['db']);//创建一个mysqli对象
    } 

    //条件
    public function where($cond)
    {
        if (isset($cond)) { 
            self::$whe = ' where '.$cond;
        }
        return $this;
    }

    //分页
    public function limit($l)
    {
        if (isset($l)) {
            self::$lim = ' limit '.$l;
        }
        return $this;
    }

    //排序
    public function order($odr)
    {
        self::$order_by = ' order by '. $odr;
        return $this;
    }
    //查询语句
    public function sql_select()
    {
        //准备一条SQL语句
        return "select * from ".self::$tab.self::$order_by.self::$whe;
    }

    //查询多条语句
    public function select()
    {
        $reslut = mysqli_query($this->mysql_connect(),$this->sql_select().self::$lim);//也可以这么写:$this->mysql_connect()->query($this->sql_select().self::$lim);
        $attr = $reslut->fetch_all(MYSQL_ASSOC);//MYSQL_ASSOC带数据库索引 MYSQL_NUM数值维索引 MYSQL_BOTH数值索引、数据库索引都有
        return $attr;
    }
   
   //查询单条语句
   public function find()
   {
        $reslut = $this->mysql_connect()->query($this->sql_select());
        $attr = $reslut->fetch_row(MYSQL_ASSOC);
        return $attr;
   }

   //添加或修改字段
   public function data(Array $data)
   {
        if (!empty($data)) {
            
            foreach ($data as $key => $value) {
                //添加的数据
                $this->str_key[] = $key;
                $this->str_val[] = $value;
                //修改的数据
                $this->key_val[] = is_string($value) ? $key.'= "'.$value.'"' : $key.'='.$value;  
            }
            return $this;
        }
    }

   //添加字段
   public function insert()
   {    

        if (!isset($this->str_key) or !isset($this->str_val)) {
            return 'error';
        }else{
            $key = implode(',', $this->str_key);
            $val = implode(',', $this->str_val);
            $mysql_content = 'insert into '.self::$tab.' ('.$key.') values('.$val.')';
            $reslut =$this->mysql_connect()->query($mysql_content);
            return $reslut;
        }
   }

   //修改字段
   public function update()
   {
        if (!$this->key_val) {
            return 'error';
        }else{
            $k_v = implode(', ',$this->key_val);
            $mysql_content = 'update '.self::$tab.' set '.$k_v.' '.self::$whe;
            $reslut =$this->mysql_connect()->query($mysql_content);
            return $reslut;
        }
   }

   //删除字段
   public function delete()
   {
        $mysql_content = 'delete from '.self::$tab.self::$whe;
        $reslut =$this->mysql_connect()->query($mysql_content);
        return $reslut;
   }

}
?>

下面是调用这个类

<?php 
@include('./model.php');
//单条查询
 $test = Db::table('test')->where('id=3')->find();
 var_dump($test);
//分页查询
// $test1 = Db::table('test')->limit('0,2')->select();
// var_dump($test1);
//多条查询
// $test2 = Db::table('test')->order('id asc')->select();
// var_dump($test2);
//添加
// $test3 = Db::table('test')->data(['i'=>3,'age'=>6,'sex'=>9])->insert();
// var_dump($test3);
// 修改
// $test4 = Db::table('test')->where('id=30')->data(['IDinfo'=>789,'newid'=>456])->update();
// var_dump($test4);
// 删除
// $test5 = Db::table('test')->where('id>11')->delete();
// var_dump($test5);
?>

 

posted @ 2018-12-14 13:48  王彭雷  阅读(803)  评论(0编辑  收藏  举报