单例模式
1.只能有一个实例。
2.必须自行创建这个实例。
3.必须给其他对象提供这一实例
<?php /** * Created by PhpStorm. * User: ASUS * Date: 2019/4/6 * Time: 19:18 */ class DB { private $_pdo; private $_table; private $_field = "*"; private $_where = ""; private $_order; private $_join; private $_sql; private function __construct() { $this->_pdo = new PDO("mysql:host=127.0.0.1;dbname=pmp",'root','root'); } private function __clone() { // TODO: Implement __clone() method. } private static $_class; public static function getClass(){ if(self::$_class==''){ self::$_class = new DB(); } return self::$_class; } /* * 设置表名 * @param string $table 表名 * */ public function table(string $table){ $this->_table = $table; $this->_sql = "$table"; return $this; } /* * 设置查询的字段 * @param string $fiele 字段名 * */ public function field(string $fiele){ $this->_field = $fiele; return $this; } /* * 设置查询条件 * @param string $where 查询条件 * */ public function where(string $where){ $this->_where = $where; $this->_sql.=" where $where"; return $this; } /* * 设置排序查询 * @param string $field 字段名 * @param string $order 排序方式,默认正序 * */ public function order(string $field,string $order = 'asc'){ $this->_order = "order by $field $order"; $this->_sql.=" order by $field $order"; return $this; } /* * 设置分组查询条件 * @param string $group 分组查询条件 * */ public function group(string $group){ $this->_sql.=" GROUP BY $group"; return $this; } /* * 设置分页查询条件 * @param int $offset 偏移量 * @param int $limit 每页条数 * */ public function limit(int $offset,int $limit){ $this->_sql.=" limit $offset,$limit"; return $this; } /* * 关联查询,参数依次为:关联表名、关联条件、链接方式(默认为inner,可传入left|right) * @param string $table 关联表明 * @param string $join 关联条件 * @param string $dir 链接方式(默认为inner,可传入left|right) * */ public function join(string $table,string $join,string $dir='inner'){ $table2 = substr($table,0,1); $this->_join.=" $dir JOIN $table as $table2 ON $join"; return $this; } /* * 查询多条数据,返回二维数组 * @return array 二维数组 * */ public function select(string $sql=''){ if(!empty($sql)){ $this->_sql = $sql; }else{ if(!empty($this->_join)){ $table1 = substr($this->_table,0,1); $this->_sql = str_replace("$this->_table","",$this->_sql); $this->_sql = "select $this->_field from ".$this->_table." as $table1 ".$this->_join." $this->_sql"; }else{ $this->_sql = "select $this->_field from ".$this->_sql; } } return $this->_pdo->query($this->_sql)->fetchAll(PDO::FETCH_ASSOC); } /* * 查询单条数据,返回一维数组 * @return array 一维数组 * */ public function find(){ if(!empty($this->_join)){ $table1 = substr($this->_table,0,1); $this->_sql = str_replace("$this->_table","",$this->_sql); $this->_sql = "select $this->_field from ".$this->_table." as $table1 ".$this->_join." $this->_sql"; }else{ $this->_sql = "select $this->_field from ".$this->_sql; } // return $this->_sql; return $this->_pdo->query($this->_sql)->fetch(PDO::FETCH_ASSOC); } /* * 添加单条数据,返回影响记录数,参数为key=>value 格式的一维数组 * @param array $data 添加的数组 * @return int 影响记录的行数 * */ public function add(array $data){ $sql = "insert into $this->_table set "; foreach ($data as $key=>$value){ $sql.="$key='$value',"; } return $this->_pdo->exec(substr($sql,0,-1)); } /* * 删除数据,返回影响记录数 * @return int 影响记录的行数 * */ public function delete(string $sql=''){ if(empty($sql)){ $sql = "delete from $this->_table WHERE $this->_where"; } return $this->_pdo->exec($sql); } /* * 修改数据,返回影响记录数,参数为key=>value格式的一维数组 * @param array $data 修改的数组 * @return int 影响记录的行数 * */ public function update(array $data){ $sql = "update $this->_table set "; foreach ($data as $key=>$value){ $sql.="$key='$value',"; } $sql = substr($sql,0,-1); $sql.=" where $this->_where"; return $this->_pdo->exec($sql); } } $obj = DB::getClass(); $data = $obj->table("city")->field("id,name")->where("id<5")->order("id",'desc')->select(); //var_dump($data); //var_dump($obj->table("city")->add(['name'=>'陕西','pid'=>0])); //var_dump($obj->table("city")->where("id in (34,36)")->delete()); //var_dump($obj->table("city")->where("id=33")->update(['name'=>'陕西','pid'=>0])); //var_dump($obj->table("city")->field("c.name,u.id")->join("user","c.id=u.id")->join("score","s.s_id=c.id")->where("u.id=1")->select()); //var_dump($obj->table("score")->group("s_id")->limit(2,3)->select());