├── README.md ├── composer.json └── src └── mysql.php /README.md: -------------------------------------------------------------------------------- 1 | # 一个简单的PHP Mysql数据库操作类 2 | 3 | ## 依赖 4 | - pdo 5 | 6 | ## 安装 7 | 1. composer 安装 ``` composer require ninvfeng/mysql ``` 8 | 2. 引入/vendor目录下的autoload.php ``` require 'vendor/autoload.php'; ``` 9 | 10 | ## 初始化 11 | ``` 12 | //配置 13 | $config=[ 14 | 'host'=>'127.0.0.1', 15 | 'port'=>3306, 16 | 'name'=>'test' 17 | ]; 18 | 19 | //推荐使用函数进行实例化,后续操作更加方便 20 | function db($table='null') use $config{ 21 | static $_db; 22 | if(!$_db){ 23 | $_db=new \ninvfeng\mysql($config); 24 | } 25 | return $_db->table($table); 26 | } 27 | ``` 28 | ### 增 29 | ``` 30 | db('user')->insert(['user'=>'ninvfeng','pass'=>'password']); 31 | db('user')->insert(['user'=>'lvlv','pass'=>'password']); 32 | ``` 33 | 34 | ### 删 35 | ``` 36 | db('user')->where(['user'=>'ninvfeng'])->delete(); 37 | ``` 38 | 39 | ### 改 40 | ``` 41 | db('user')->where(['user'=>'lvlv'])->update(['pass'=>'password2']); 42 | ``` 43 | 44 | ### 查找一条 45 | ``` 46 | db('user')->where(['user'=>'lvlv'])->find(); 47 | ``` 48 | 49 | ### 查找全部 50 | ``` 51 | db('user')->select(); 52 | ``` 53 | 54 | ### 条件查找 55 | ``` 56 | db('user')->where(['user'=>'ninvfeng'])->select(); 57 | ``` 58 | 59 | ### 分页查找 60 | ``` 61 | db('user')->page(1)->select(); 62 | ``` 63 | 64 | ### 字段查找 65 | ``` 66 | db('user')->field('user')->select(); 67 | ``` 68 | 69 | ### 排序 70 | ``` 71 | db('user')->order('id desc')->select(); 72 | ``` 73 | 74 | ### join 75 | ``` 76 | db('user')->join('user_info on user_info.user_id=user.id')->select(); 77 | ``` 78 | 79 | ### debug 仅打印sql不执行 80 | ``` 81 | db('user')->debug()->select(); 82 | ``` 83 | 84 | ### 执行原生sql 85 | ``` 86 | db('user')->query("select * from user"); 87 | ``` 88 | 89 | ### 事务 90 | ``` 91 | db('user')->trans(); 92 | ``` 93 | 94 | ### 返回原生对象 95 | ``` 96 | db()->pdo(); 97 | ``` 98 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ninvfeng/mysql", 3 | "description": "一个简单PDO mysql数据库操作类", 4 | "license":"MIT", 5 | "version":"1.0", 6 | "keywords" : [ "php", "mysql" ], 7 | "authors": [ 8 | { 9 | "name": "ninvfeng", 10 | "email": "ninvfeng@qq.com" 11 | } 12 | ], 13 | "require": { 14 | "php": ">=7.0.0" 15 | }, 16 | "autoload": { 17 | "psr-4": { 18 | "ninvfeng\\": "src/" 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/mysql.php: -------------------------------------------------------------------------------- 1 | _pdo=new \PDO('mysql:host='.$config['host'].';dbname='.$config['name'],$config['user'],$config['pass'],array(\PDO::ATTR_PERSISTENT => true)); 19 | 20 | //设置客户端字符集 21 | $this->_pdo->exec("set names 'utf8'"); 22 | 23 | //禁用prepared statements的仿真效果 确保SQL语句和相应的值在传递到mysql服务器之前是不会被PHP解析 24 | $this->_pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false); 25 | 26 | //数据表 27 | $this->_table=$table; 28 | } 29 | 30 | //返回pdo对象 31 | public function pdo(){ 32 | return $this->_pdo; 33 | } 34 | 35 | //操作表 36 | public function table($table){ 37 | $this->_table=$table; 38 | return $this; 39 | } 40 | 41 | //字段 42 | public function field($field){ 43 | $this->_field=$field; 44 | return $this; 45 | } 46 | 47 | //排序 48 | public function order($order){ 49 | $this->_order='order by '.$order; 50 | return $this; 51 | } 52 | 53 | //限制 54 | public function limit($limit){ 55 | $this->_limit='limit '.$limit; 56 | return $this; 57 | } 58 | 59 | //条件 60 | public function where($where,$param=[]){ 61 | if($param){ 62 | $this->_param=array_merge($this->_param,$param); 63 | } 64 | if(is_array($where)){ 65 | $res=''; 66 | foreach($where as $k => $v){ 67 | $column_key=''; 68 | foreach (explode('.',$k) as $kk => $vv) { 69 | $column_key.=' `'.$vv.'`.'; 70 | $column_plac='where_'.$vv; 71 | } 72 | if(is_array($v)){ 73 | $op = ' '.$v[0].' '; 74 | $this->_param[$column_plac]=$v[1]; 75 | }else{ 76 | $op = ' = '; 77 | $this->_param[$column_plac]=$v; 78 | } 79 | $column_key=trim($column_key,'.'); 80 | $res.=$column_key.$op.':'.$column_plac.' and'; 81 | } 82 | $where=trim($res,'and'); 83 | 84 | } 85 | $this->_where.=' '.$where.' and'; 86 | 87 | return $this; 88 | } 89 | 90 | //分页 91 | public function page($page=1,$num=10){ 92 | $page=intval($page); 93 | $num=intval($num); 94 | $start=($page-1)*$num; 95 | $this->_limit="limit $start,$num"; 96 | return $this; 97 | } 98 | 99 | //join 100 | public function join($join){ 101 | 102 | //语句中不包含join时自动添加left join 103 | if(stripos($join,'join')===false){ 104 | $join='left join '.$join; 105 | } 106 | $this->_join=$join; 107 | return $this; 108 | } 109 | 110 | //调试 111 | public function debug(){ 112 | $this->_debug=true; 113 | return $this; 114 | } 115 | 116 | //结果集 117 | public function select(){ 118 | $res=$this->_query(); 119 | if($res&&count($res[0])==1){ 120 | $column=explode('.',$this->_field); 121 | $column=array_pop($column); 122 | $result=array_column($res,$column); 123 | return $result; 124 | }else{ 125 | return $res; 126 | } 127 | } 128 | 129 | //获取单条数据 130 | public function find(){ 131 | $res=$this->_query()[0]; 132 | if($res&&count($res)==1){ 133 | $column=explode('.',$this->_field); 134 | $column=array_pop($column); 135 | return $res[$column]; 136 | }else{ 137 | return $res; 138 | } 139 | } 140 | 141 | //更新 142 | public function update($data){ 143 | if($this->_where){ 144 | $update=''; 145 | foreach($data as $k => $v){ 146 | $column_key=''; 147 | foreach (explode('.',$k) as $kk => $vv) { 148 | $column_key.='`'.$vv.'`.'; 149 | $column_plac=$vv; 150 | } 151 | $this->_param[$column_plac]=$v; 152 | $column_key=trim($column_key,'.'); 153 | $update.=$column_key."=:".$column_plac.","; 154 | } 155 | $update=trim($update,','); 156 | $this->preWhere(); 157 | $this->_sql="update {$this->_table} set $update {$this->_where};"; 158 | return $this->exec($this->_sql,$this->_param); 159 | }else{ 160 | echo '保存数据需指定条件'; 161 | die(); 162 | } 163 | } 164 | 165 | //添加 166 | public function insert($data){ 167 | $update=''; 168 | foreach($data as $k => $v){ 169 | $column_key=''; 170 | foreach (explode('.',$k) as $kk => $vv) { 171 | $column_key.='`'.$vv.'`.'; 172 | $column_plac=$vv; 173 | } 174 | $this->_param[$column_plac]=$v; 175 | $column_key=trim($column_key,'.'); 176 | $update.=$column_key."=:".$column_plac.","; 177 | } 178 | $update=trim($update,','); 179 | $this->_sql="insert into {$this->_table} set $update;"; 180 | $this->exec($this->_sql,$this->_param); 181 | return $this->_pdo->lastInsertId(); 182 | } 183 | 184 | //删除 185 | public function delete(){ 186 | if($this->_where){ 187 | $this->preWhere(); 188 | $this->_sql="delete from {$this->_table} {$this->_where};"; 189 | return $this->exec($this->_sql,$this->_param); 190 | }else{ 191 | echo '删除数据需指定条件'; 192 | die(); 193 | } 194 | } 195 | 196 | //执行原生query 197 | public function query($sql,$param=[]){ 198 | $this->clearParam(); 199 | if($this->_debug){ 200 | echo "
";
201 |             echo $this->debugSql();
202 |             die();
203 |         }else{
204 |             $pre=$this->_pdo->prepare($sql);
205 |             if(!$pre){
206 |                 $this->_error();
207 |             }
208 |             $pre->execute($param);
209 |             if($this->_error()){
210 |                 return $pre->fetchAll(\PDO::FETCH_ASSOC);
211 |             }
212 |         }
213 |     }
214 | 
215 |     //执行原生exec
216 |     public function exec($sql,$param=[]){
217 |         $this->clearParam();
218 |         if($this->_debug){
219 |             echo "
";
220 |             echo $this->debugSql();
221 |             die();
222 |         }else{
223 |             $pre=$this->_pdo->prepare($sql);
224 |             $res=$pre->execute($param);
225 |             if($this->_error()){
226 |                 return $res;
227 |             }
228 |         }
229 |     }
230 | 
231 |     //事务
232 |     public function trans($callback,$arr=[])
233 |     {
234 |         $this->_pdo->beginTransaction();
235 |         try {
236 |             $result = null;
237 |             if (is_callable($callback)) {
238 |                 $result = call_user_func_array($callback, [$arr]);
239 |             }
240 |             $this->_pdo->commit();
241 |             return $result;
242 |         } catch (\Exception $e) {
243 |             $this->_pdo->rollback();
244 |             throw $e;
245 |         }
246 |     }
247 | 
248 |         //清空参数
249 |     public function clearParam(){
250 |         $this->_field='*';
251 |         $this->_where='';
252 |         $this->_order='';
253 |         $this->_limit='';
254 |         $this->_join='';
255 |         $this->_debug=false;
256 |         $this->_param=[];
257 |         $this->_sql='';
258 |     }
259 | 
260 |     //自增
261 |     public function setInc($field,$step=1){
262 |         if($this->_where){
263 |             $update=$field.'='.$field.'+'.$step;
264 |             $this->preWhere();
265 |             $this->_sql="update {$this->_table} set $update {$this->_where};";
266 |             return $this->exec($this->_sql,$this->_param);
267 |         }else{
268 |             echo '保存数据需指定条件';
269 |             die();
270 |         }
271 |     }
272 | 
273 |     //自减
274 |     public function setDec($field,$step=1){
275 |         if($this->_where){
276 |             $update=$field.'='.$field.'-'.$step;
277 |             $this->preWhere();
278 |             $this->_sql="update {$this->_table} set $update {$this->_where};";
279 |             return $this->exec($this->_sql,$this->_param);
280 |         }else{
281 |             echo '保存数据需指定条件';
282 |             die();
283 |         }
284 |     }
285 |     
286 |     //预处理where条件
287 |     protected function preWhere(){
288 |         if($this->_where){
289 |             $this->_where='where'.trim($this->_where,'and'); 
290 |          }
291 |          return $this;
292 |     }
293 | 
294 |     //查询
295 |     protected function _query(){
296 |         $this->preWhere();
297 |         $this->_sql="select {$this->_field} from {$this->_table} {$this->_join} {$this->_where} {$this->_order} {$this->_limit}";
298 |         return $this->query($this->_sql,$this->_param);
299 |     }
300 | 
301 |     //错误处理
302 |     protected function _error(){
303 |         if($this->_pdo->errorCode()==00000){
304 |             return true;
305 |         }else{
306 |             echo '
';
307 |             $error_msg=$this->_pdo->errorInfo()[2];
308 |             $e=new \Exception($error_msg);
309 |             echo '

'.$error_msg.'

'; 310 | echo '

'.$e->getTrace()[2]['file'].' In line '.$e->getTrace()[2]['line'].'

'; 311 | echo '

SQL 语句:'.$this->debugSql().'

'; 312 | die(); 313 | } 314 | } 315 | 316 | //生成调试sql 317 | protected function debugSql(){ 318 | $res=$this->_sql; 319 | foreach ($this->_param as $k => $v) { 320 | $res=str_replace(':'.$k,'"'.$v.'"',$res); 321 | } 322 | return $res; 323 | } 324 | 325 | } 326 | --------------------------------------------------------------------------------