1 <?php
2 class six{
3 //定义私有静态变量存储 实例化对象
4 private static $ob;
5 //定义表名
6 private $table;
7 //定义条件
8 private $where;
9 //定义公共pdo变量用于存储 链接数据库配置实例
10 public $pdo;
11 //定义私有最终构造方法 防止 继承 子类重写 ;防止直接实例化对象
12 private function __construct()
13 {
14 $this->pdo = new PDO("mysql:host=127.0.0.1;dbname=blog","root","root");
15
16 }
17 //定义私有克隆 防止类外克隆
18 private function __clone()
19 {
20 echo "no1";
21 }
22 //私有静态方法 用于实例化类对象
23 public static function gits(){
24 //判断如果静态属性中是否有类对象 如果没有则 实例化该类 赋予 静态属性
25 //如此便开辟了一块静态空间用来存储实例化对象 类外只需调用一次即可 节省了资源
26 // 实现了单例模式的作用
27 if (!(self::$ob instanceof self)){
28
29 self::$ob = new self();
30
31 }
32
33 return self::$ob;
34
35 }
36
37 //单删 批删封装
38 public function deleteAll($id){
39 //判断$id 是否有值
40 if (empty($id)){
41
42 return false;
43 }
44 //取数组中 values值
45 $key = array_values($id);
46 //取得健为零值为要删除数据值的一维数组
47 @$num = count($key[0]);
48 //循环 数组$id
49 foreach ($id as $k => $v){
50 //$str = 数据库主键id字段
51 $str = $k;
52 // idss = 要删除的数据id
53 $idss = $v;
54
55 }
56 $ids = ' where ';
57 //判断 如果要删除的数据为数组并大于一条 则拼接为 id in (11,12,13)形式
58 if (is_array($id) && $num > 1){
59
60 $ids .= $str ." in " . '('.implode(",",$idss) .')';
61 //数组等于1条(这个count)值是判断 要删除的数据个数为1
62 //拼接成 id=1 的形式
63 }else if (is_array($id) && count($id) == 1){
64
65 $ids .= $str . ' = ' . $idss ;
66
67 }
68 //返回 实现单删多删
69 return $this->pdo->exec("delete from $this->table $ids");
70 }
71
72 //修改
73
74 public function update($data){
75
76 $str = '';
77 //循环数组拼接成 id=1,name=小明的形式
78 foreach ($data as $k => $v){
79
80 $str .= "," . $k .'='. "'$v'";
81 }
82 $str = substr($str,1);
83
84 //返回数组实现修改
85 return $this->pdo->exec("update $this->table set $str $this->where");
86
87 }
88
89 //定义传入的表名
90 public function tables($table){
91
92 $this->table = $table;
93 return $this;
94
95 }
96
97 public function where($where){
98
99 if (empty($where)){
100
101 return false;
102 }
103 $str = ' where ';
104 if (is_array($where)){
105
106 foreach ($where as $k => $v){
107
108 $str .= $k .' = '. "'$v'" . ' and ';
109 }
110 $str = rtrim($str,' and ');
111
112 }else{
113
114 $str .= $where;
115
116 }
117 return $this->where = $str;
118
119 }
120
121 //单挑查询
122 public function get(){
123
124 return $this->pdo->query("select * from $this->table $this->where")->fetch(PDO::FETCH_ASSOC);
125
126 }
127
128 //添加处理数据
129 public function insert($data){
130 //找到key 循环遍历成 id,name,sex 的形式
131 $keyArr = array_keys($data);
132 $str = '';
133 foreach ($keyArr as $k => $v){
134
135 $str .= "," . $v;
136 }
137 $str = substr($str,1);
138 //找到 value 遍历成 '1','小明','男' 的形式
139 $valArr = array_values($data);
140 $strs = '';
141 foreach ($valArr as $kk => $vv){
142 $strs .= "," . "'$vv'";
143 }
144 $strs = substr($strs,1);
145
146 // 执行则添加成功
147 return $this->pdo->exec("insert into $this->table ($str) values ($strs)");
148
149 }
150
151 }
152
153 $ob = six::gits();
154
155 //单查询
156 //$ob->tables('student')->where(['s_id'=>'08','s_name'=>'王菊']);
157 //$data = $ob->get();
158 //添加
159 //$data = $ob->tables('student')->insert(['s_id'=>'14','s_name'=>'赵雷','s_birth'=>'1990-01-01','s_sex'=>'男']);
160 //删除(单删 批删)
161 //$data = $ob->tables('student')->deleteAll(['s_id'=>14]);
162 //修改
163 $ob->tables('student')->where(['s_id'=>'08']);
164 $data = $ob->update(['s_name'=>'李云','s_sex'=>'男']);
165
166 var_dump($data);