mysql Model

 PHP Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php

     class  mysql{
        
public  $table;   //用于存储表名
         public  $link;
        
public  $sql;         //存储SQL语句
         public  $field =  ' * ' ;   //保存字段
         public  $where;
        
public  $order;
        
public  $limit;   // 查询显示数量

        
function   __construct ($hostname,$username,$password,$dbDataName){
            
//构造函数 初始化...链接数据库
             // echo '构造函数 初始化...链接数据库'.$hostname,'<br/>';
            $this->connect($hostname,$username,$password);
            
//选择数据库
            $this->select_db($dbDataName);
        }

        
function   __destruct (){
            
//析构函数
            mysqli_close($this->link);
        }
//------------ 连贯操作函数
         public   function  table($table){
            $this->table = $table;
            $this->where = 
null ;   // 清除上次使用的条件语句 新查询
             return   $this ;
        }
        
//字段过滤 字段筛选
         //设置字段
         public   function  field($field,$isCheck= true ){
            
//判断传递的参数是否是数组
             if (!is_array($field)){
                $fields = func_get_args();
                
if (is_array($fields)){
                    $this->field = join(
',' ,$fields);
                }
                
// var_dump($this->field);
                 return   $this ;
            }
            
//是否检测字段数据
             if ($isCheck){
                
//检测数据内容 删除数据库中没有的字段
                $fields = $this->check($field);
            }
            
//可能你写的数组里面都不在我们字段列表中 返回的是一个空数组所有我们需要再次判断
             if (!is_array($fields) ||  empty ($fields)){
                
//连贯操作
                 return   $this ;
            }
            $this->field = join(
',' ,$fields);
            
//连贯操作
             return   $this ;
        }
// 每页显示多少条
         public   function  limit($limit){
            $this->limit = 
' limit ' .$limit;
            
return   $this ;
        }
//排序
         public   function  order($str){
            $this->order = 
"ORDER BY {$str} " ;
            
return   $this ;
        }

        
// where 条件查询语句
         //$data['id']=1;
         //$data[id]=array('lt',10);
         //$data['name']=array('like','长');
         //$data['id']=array('in','1,2,3,4,5,6');
         public   function  where($data){
            
//判断传递过来的必须是一个数组 而且是不为空的数组
             if (is_array($data) && !empty($data)){
                
//接收的结果
                $result =  array ();
                
//循环传递进来的数组得到里面的键和值
                 foreach ($data  as  $key=>$val){
                    
//判断值是否是一个数组
                     if (is_array($val)){
                        $type = $val[
0 ];
                        
//判断你是什么样的条件 将你所要的条件进行拼接
                         switch ($type){
                                
case   'lt' :
                                    $result[] = 
"{$key}<{$val[1]}" ;
                                
break ;
                                
case   'like' :
                                    $result[] = 
"{$key} LIKE '%{$val[1]}%'" ;
                                
break ;
                                
case   'gt' :
                                    $result[] = 
"{$key}>{$val[1]}" ;
                                
break ;
                                
case   'in' :
                                    $result[]= 
"{$key} in({$val[1]})" ;
                                
break ;
                            
default :
                                
// echo 'where invalid';
                                 break ;
                        }
                    }
else {
                        $result[] = 
" {$key}='{$val}'" ;
                    }
                }
            
//最终得到的数组样式
             //var_dump($result);

            
//将数组和where拼接 得到最终的where效果
            $where =  ' where ' .join( ' and  ' ,$result);
            
//echo $where;
             //写入属性
            $this->where = $where;

            }

            
return   $this ;
        }
//----------------选择查询数据
         public   function  select(){
            $this->sql = 
"SELECT {$this->field} FROM {$this->table} {$this->where} {$this->order} {$this->limit} " ;
            
return  $this->query($this->sql);
        }
        
// 查询一条结果
         public   function  find($id= null ){
            
if ( empty ($id)){
                
// where 条件查询
                $where = $this->where;
            }
else {
                
//通过 id 查询
                $where =  'WHERE id=' .$id;
            }
            $this->sql = 
"SELECT {$this->field} FROM {$this->table} {$where}" ;
            $list = $this->query($this->sql);
            
return  $list[ 0 ];
        }
//----------------添加数据
         public   function  add($data){
            
//获取数组中所有的键并组合
            $keys = join( ',' ,array_keys($data));
            
//获取数组中所有的值并组合
            $values = join( "','" ,array_values($data));
            
//组合sql数据库语句
            $this->sql =  "INSERT INTO {$this->table}($keys) VALUES('{$values}')" ;
            
//执行sql语句
             return  $this->execute($this->sql);
        }
//  获取表中的字段信息
         public   function  getField(){
            $sql = 
"DESC {$this->table}" ;
            $arr = $this->query($sql);
            $fields = 
array ();
            
foreach ($arr  as  $value){
                $fields[] = $value[
'Field' ];
            }
            
return  $fields;
        }
        
//用来检测数据库中是否有指定字段的方法
         public   function  check($arr){
            
// 获取数据库中的字段
            $field = $this->getField();
            
foreach ($arr  as  $key=>$val){
                
//判断 你的值是否在数据库的字段中
                 if (!in_array($val,$field)){
                    
//删除传进来无用的字段
                    unset($arr[$key]);
                }
            }
            
// 返回过滤后的新数组字段
             return  $arr;

        }
//  统计总条数 总记录数
         public   function  count(){
            $this->sql = 
"SELECT COUNT(*) total FROM {$this->table}" ;
            $total = $this->query($this->sql);
            
return  $total[ 0 ][ 'total' ];
        }
// 更新数据
         public   function  update($arr){
            
//判断 $arr 是不是数组
             if (!is_array($arr)){
                
return   false ;
            }
            
//判断 你是否使用id修改 还是使用 where添加修改
             if (!empty($arr[ 'id' ])){
                $where = 
' WHERE id=' .$arr[ 'id' ];
            }
else {
                $where = $this->where;
            }

            $result = 
'' ;
            
foreach ($arr  as  $key=>$val){
                
//反引号是用来判处你使用的字段为关键字的问题
                 if ($key !=  'id' ){
                    $result .= 
"`{$key}`='{$val}'," ;
                }
            }
            
//删除多出来的一个逗号
            $result = rtrim($result, ',' );
            $this->sql = 
"UPDATE {$this->table} SET {$result} {$where}" ;
            
//执行 sql 语句
             return  $this->execute($this->sql);
        }

        
//删除数据
         public   function  del($id){
            $this->sql = 
"DELETE FROM {$this->table} WHERE id='{$id}'" ;
            
return  $this->execute($this->sql);
        }


        
// 辅助方法
         protected   function  connect($hostname,$username,$password){
            
// 连接数据库
            $this->link = mysqli_connect($hostname,$username,$password);
            
// 判断错误
             if (mysqli_connect_errno($this->link)){
                
// 失败返回连接错误代码
                 return  mysqli_connect_error($this->link);
            }

        }
        
//选择数据库
         protected   function  select_db($dbDataName){
            
//选择数据库
            mysqli_select_db($this->link,$dbDataName);
            
//设置字符集
            mysqli_set_charset($this->link, 'utf8' );

        }
        
// 建表语句
         protected   function  newTable($sql){
            
// 用于创建表
             return  mysqli_query($this->link,$sql);
        }
        
// 执行sql 语句
         protected   function  execute($sql){   // insert update delete
             // 执行sql语句
            $result = mysqli_query($this->link,$sql);
            
// 检查执行结果  检查 INSERT, UPDATE, or DELETE
             if ($result && mysqli_affected_rows($this->link)> 0 ){
                
// 执行成功返回上次插入的id 或者 受影响的id
                 return  mysqli_insert_id($this->link) ? mysqli_insert_id($this->link) : mysqli_affected_rows($this->link);
            }
            
return   0 ;
        }
        
// 查询sql 语句
         protected   function  query($sql){  // select
             // 查询
            $result = mysqli_query($this->link,$sql);
            
//检查查询的结果并返回中记录数
             if ($result && mysqli_num_rows($result)> 0 ){
                $list = 
array ();
                
//将查询得到的结果以关联数组的形式返回 每次返回一条
                 while ($row = mysqli_fetch_assoc($result)){
                    $list[] = $row;
                }
                mysqli_free_result($result);
                
return  $list;
            }

        }

    }

?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值