bd.php

<?php /* explain: version : 1.0.1 使用: $db = new db; (1) : 查询操作 : 返回数组. 查询表里所有的数据: $db->table('price_table')->select(); 查看表里指定的字段: $db->table('price_table')->select('name,price'); 查询带有子句(条件)数据: $db->table('price_table')->where('id>5 or price > 80')->select(); $db->table('price_table')->where('i>3')->group('name')->order('price desc')->limit('3,5')->select('name,sum(price) as sum'); (2) : 查询数据总条数 $db->table('price_table')->count(); $db->table('price_table')->where('id>5')->count(); (3) : 删除数据: $res = $db->table('price_table')->where('id=5')->delete(); 返回boolean (4) : 更新数据(修改) $arr = array('name'=>'萝卜','price'=>888); $res = $db->table('price_table')->where('id=5')->update($arr); 返回boolean (5) : 插入数据(增加) $arr = array('name'=>'土豆','price'=>88); $res = $db->table('price_table')->insert($arr); 返回插入的那条数据的id */ class db{ private $_db = null; //数据库连接句柄 mysqli_connect() private $_table = null; //查询表名 class_table private $_where = null; //where条件 where id > 5 private $_order = null; //order排序 order by price desc private $_limit = null; //limit限定查询 limit 3,5 private $_group = null; //groups合并 group by name private $_configs = array( //连接数据库的默认值(数据库默认配置) 'hostname' => 'localhost', 'dbname' => 'fourexam', 'username' => 'root', 'password' => 'root' ); //构造函数: function __construct(){ //连接数据库 $link = mysqli_connect( $this->_configs['hostname'], $this->_configs['username'], $this->_configs['password'], $this->_configs['dbname'] ); if( !$link ){ //连接失败 //返回一个带有错误信息的字符串. $this->showError('错误信息'.mysqli_connect_error()); die(); } mysqli_query($link,'set names utf8'); $this->_db = $link; } //设置要操作的表: public function table($table){ $this->_table = $table; return $this; } //where条件判断 public function where($where){ $whereStr = ''; //存储条件判断字符串 if( is_string($where) && !empty($where) ){ $whereStr = 'WHERE '.$where;//WHERE id=5 and name='xxx' }else if( is_array($where) ){ //array('name'=>'张三','price'=>60) name='张三' and price=60 foreach( $where as $key=>$value ){ //$key=$value and $key=$value //name='张三' and price=60 if( end($where) == $value ){ //最后一项了 $whereStr .= $key .'="'.$value.'" '; }else{ $whereStr .= $key.'="'.$value.'" AND '; } } $whereStr = 'WHERE '.$whereStr; } $this->_where = $whereStr; return $this; } //group合并 public function group($group=''){ $groupStr = ''; if( is_string($group) && isset($group) ){ $groupStr = 'GROUP BY '.$group;//GROUP BY name } $this->_group = $groupStr; return $this; } //order排序 public function order($order=''){ $orderStr = ''; if( is_string($order) && isset($order) ){ $orderStr = 'ORDER BY '.$order;//ORDER BY price desc } $this->_order = $orderStr; return $this; } //limit限制 public function limit($limit=''){ $limitStr = ''; if( is_string($limit) && isset($limit) ){ $limitStr = 'LIMIT '.$limit;//LIMIT 3,5 } $this->_limit = $limitStr; return $this; } //查询操作: public function select($fields='*'){ $fieldsStr = '*'; //声明要查询的字段 if( is_string($fields) && !empty($fields) ){ $fieldsStr = $fields; }else if( is_array($fields) ){ //如果传进来的是数组,通过implode方法 用 , 把数组拼接为字符串 $fieldsStr = implode(',',$fields); } //设置语句: $sql = "SELECT {$fieldsStr} FROM {$this->_table} {$this->_where} {$this->_group} {$this->_order} {$this->_limit}"; //执行语句: $re = $this->execute($sql); $data = array(); //存储数据 if( !empty($re) ){ while( $row = mysqli_fetch_assoc($re) ){ $data[] = $row; } } return $data; } //查询数据总条数 public function count(){ $sql = "SELECT COUNT(*) AS n FROM {$this->_table} {$this->_where}"; //执行sql $re = $this->execute($sql); $data = array('n' => 0); if( !empty($re) ){ $data = mysqli_fetch_assoc($re); } return $data['n']; } //删除数据 public function delete(){ $sql = "DELETE FROM {$this->_table} {$this->_where}"; $re = $this->execute($sql); return $re; } //更新数据(修改) //update price_table set name="别的",price=88 where id=11; //$db->table('price_table')->where('id=11')=>update('name="别的",price=88'); //$arr = array('name'=>'别的','price'=>88); 转 name="别的",price=88 //$db->table('price_table')->where('id=11')=>update($arr); public function update($data=''){ $dataStr = ''; if( is_string($data) && isset($data) ){ $dataStr = $data; }else if( is_array($data) ){ //array('name'=>'别的','price'=>88); 转 name="别的",price=88 foreach( $data as $key=>$value ){ $dataStr .= $key.'="'.$value.'",'; } //去除字符串右边的 , $dataStr = rtrim($dataStr,','); } //更新语句 $sql = "UPDATE {$this->_table} SET {$dataStr} {$this->_where}"; $re = $this->execute($sql); return $re; } //插入数据 //INSERT INTO price_table (name,price) VALUES('新的',10); //$arr = array('name'=>'新的',price=>100); //$db->table('price_table')->insert($arr); public function insert($data=''){ $keys = ''; $values = ''; if( is_array($data) ){ // $keys = implode(',',array_keys($data)); // $values = implode(',',array_values($data)); foreach( $data as $key=>$value ){ $keys .= $key.','; // name,price, $values .= '"'.$value.'",'; // "新的","90", } $keys = rtrim($keys,','); $values = rtrim($values,','); } $sql = "INSERT INTO {$this->_table} ({$keys}) VALUES({$values})"; $this->execute($sql); //返回插入的那条数据的ID,返回上一步INSERT 操作产生的id $insertId = mysqli_insert_id($this->_db); return $insertId; } //执行sql语句(不管增加 删除 修改 查询)都在这执行 private function execute($sql=null){ $res = mysqli_query($this->_db,$sql); if( !$res ){ //sql语句执行失败, 用mysqli_error()显示错误信息 echo '此SQL有毒'.$sql.','.mysqli_error($this->_db); return null; } //无论做了任何操作 ,要把属性的值清空,防止影响下一次的调用操作 $this->_table = null; $this->_where = null; $this->_group = null; $this->_order = null; $this->_limit = null; return $res; } //数据库异常输出 private function showError($msg){ echo '
'.$msg.'
'; } } ?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值