使用过ThinkPHP框架的同学可能会对于其中数据库模型操作特别有好感,ThinkPHP提供了数据库操作的简单的操作,对于连接数据库,数据库的增删改查等数据操作都非常的nice,同时支持连贯操作,对于那些不习惯写sql语句的同学真是大大的便利。(注:sql还是很重要的,不要因为用了框架就把原先的忘了)。
而在笔者使用php操作redis实现后台任务的过程中,也想要借助这种便利,但无奈redis操作单独的类,直接访问其中的controller文件的话,总是会提示M方法失败,导致此模型方法不能使用。万般无奈之下,只能自己来实现一下了。
借助PHP的mysqli相关函数,进行MySQL数据库操作类的实现。此程序中提供数据库的操作包括:数据库的连接,数据库的选择,数据库用户的选择,相应数据库中所有数据表名的查看;数据表的操作包括:相应的数据表中字段全部属性的查看,数据表的增删改查操作,数据表查询、插入的连贯操作等
_initialize(); if(!isset($host)||!isset($user)||!isset($passowrd)||!isset($database)){ return false; }else{ $this->_host = $host; $this->_user = $user; $this->_password = $passowrd; $this->_database = $database; $this->_port = $port; $_dbObj = new mysqli($host,$user,$passowrd,$database,$port); if($_dbObj->connect_errno){ $this->error = $_dbObj->connect_error; return false; }else{ $this->_dbObj = $_dbObj; return $this; } } } public function error(){ return $this->error; } // 回调方法 初始化模型 protected function _initialize() {} public function __set($name,$value) { // 设置数据对象属性 $this->data[$name] = $value; } public function __get($name) { return isset($this->data[$name])?$this->data[$name]:null; } public function __isset($name) { return isset($this->data[$name]); } public function __unset($name) { unset($this->data[$name]); } public function __call($method,$args) { } public function select_db($database){ $select_db = mysqli_select_db($this->_dbObj,$database); if($select_db){ $this->_database = $database; $_dbObj = new mysqli($this->_host,$this->_user,$this->_password,$database,$this->_port); $this->_dbObj = $_dbObj; return $this; }else{ $this->error = mysqli_error($this->_dbObj); return false; } } public function change_user($user,$password){ $change_user = mysqli_change_user($this->_dbObj,$user,$password,$this->_database); if($change_user){ $this->_user = $user; $this->_password = $password; $_dbObj = new mysqli($this->_host,$this->_user,$this->_password,$this->_database,$this->_port); $this->_dbObj = $_dbObj; return $this; }else{ $this->error = mysqli_error($this->_dbObj); return false; } } public function tables(){ $sql = 'show tables'; $search_res = mysqli_query($this->_dbObj,$sql); if($search_res){ $num_rows = $search_res->num_rows; $tables_msg = array( 'count'=>$num_rows, 'tables'=>array() ); for($i=0;$i<$num_rows;$i++){ $row = $search_res->fetch_assoc(); $key = 'Tables_in_'.$this->_database; array_push($tables_msg['tables'],$row[$key]); } mysqli_free_result($search_res); return $tables_msg; }else{ mysqli_free_result($search_res); return false; } } public function select_table($table){ $sql = 'select * from '.$table; $search_res = mysqli_query($this->_dbObj,$sql); if($search_res){ $this->_table = $table; $table_msg = self::query_handle($search_res); $this->_tableObj = $table_msg; mysqli_free_result($search_res); return $table_msg; }else{ mysqli_free_result($search_res); return false; } } public function select_table_fields($table){ $sql = 'show fields from '.$table; $search_res = mysqli_query($this->_dbObj,$sql); if($search_res){ $this->_table = $table; $fields_msg = self::query_handle($search_res); mysqli_free_result($search_res); return $fields_msg; }else{ mysqli_free_result($search_res); return false; } } public function getField($field){ $fields = self::param_handle($field); $count = count($fields); for($i=0;$i<$count;$i++){ $index = $fields[$i]; $sql = 'select '.$index.' from '.$this->_table; $res = mysqli_query($this->_dbObj,$sql); $field_msg[$index] = self::query_handle($res); } return $field_msg; } protected function query_handle($obj){ $res = array(); for($i=0;$i<$obj->num_rows;$i++){ $row = $obj->fetch_assoc(); array_push($res,$row); } return $res; } public function param_handle($param){ if(is_string($param)&&!empty($param)){ $params = explode(',',$param); }elseif(is_array($param)&&!empty($param)){ $params = $param; }else{ return false; } return $params; } public function options_handle($param){ if(is_numeric($param)){ $option = $param; }elseif(is_string($param)&&!empty($param)&&!is_numeric($param)){ $params = explode(',',$param); $count = count($params); $option = implode(' and ',$params); }elseif(is_array($param)&&!empty($param)){ $params = $param; $count = count($params); $arr = array(); foreach($param as $key=>$value){ $tip = "$key=$value "; array_push($arr,$tip); } $option = implode(' and ',$arr); }else{ return false; } return $option; } protected function option(){ $options = $this->options; $option = ''; if(isset($options['where'])){ $option .= 'where '.$options['where'].' '; } if(isset($options['order'])){ $option .= 'order by '.$options['order'].' '.$options['order_type'].' '; } if(isset($options['limit'])){ $option .= 'limit '.$options['limit']; } return $option; } public function find(){ $option = self::option(); $sql = 'select * from '.$this->_table.' '.$option; $search_res = mysqli_query($this->_dbObj,$sql); $msg = self::query_handle($search_res); return $msg; } public function where($where){ $this->options['where'] = self::options_handle($where); return $this; } public function limit($limit){ $this->options['limit'] = self::options_handle($limit); return $this; } public function order($order,$type='desc'){ $this->options['order'] = $order; $this->options['order_type'] = $type; return $this; } public function data(array $data){ $values = array(); $fields = array(); if(is_array($data)){ foreach($data as $key=>$value){ if(is_array($value)){ //二维数组 $tip = 1; array_push($values,'('.implode(',',array_values($value)).')'); array_push($fields,'('.implode(',',array_keys($value)).')'); }else{ //一维数组 $tip = 0; } } }else{ return false; } if(!$tip){ array_push($values,'('.implode(',',array_values($data)).')'); array_push($fields,'('.implode(',',array_keys($data)).')'); } $this->data['fields'] = $fields[0]; $this->data['values'] = implode(',',$values); return $this; } public function add(){ $fields = $this->data['fields']; $values = $this->data['values']; $sql = 'INSERT INTO '.$this->_table.$fields.'VALUES'.$values; $res = mysqli_query($this->_dbObj,$sql); return $res; } function save(array $data){ $tip = array(); if(is_array($data)){ foreach($data as $key=>$value){ array_push($tip,"$key=$value"); } }else{ return false; } $set_msg = implode(',',$tip); $sql = 'UPDATE '.$this->_table.' SET '.$set_msg.' WHERE '.$this->options['where']; $res = mysqli_query($this->_dbObj,$sql); return $res; } public function delete(){ $sql = 'DELETE FROM '.$this->_table.' WHERE '.$this->options['where']; $res = mysqli_query($this->_dbObj,$sql); return $res; } public function query($sql){ $search_res = mysqli_query($this->_dbObj,$sql); return $search_res; } protected function sql(){ } public function close(){ $close = mysqli_close($this->_dbObj); if($close){ return true; }else{ return false; } } function __destruct(){ mysqli_close($this->_dbObj); }}
操作示例:
首先实例化此类,其中需要输入host(数据库地址)、user(数据库用户)、password(数据库用户密码)、database(数据库名称)此四种信息,进行数据库的连接,然后即可调用select_table()方法,其中传入数据表名,从而设置对指定表的操作,从而即可利用连贯操作进行相应数据的增删改查。 其中参数基本均支持字符串和数组两种形式。
include '/classes/db.php'; $db = new \Database('localhost', 'root', '901230', 'weixin'); //$db = new \mysqli('localhost','root','901230','weixin'); //$db->select_db('visitor'); //dump($db->error()); //$db->change_user('helen','901230'); $table = 'zyd_fuweng_user'; //dump($db->select_table_fields($table)); //dump($db->error()); $db->select_table($table); $param1 = '123'; $param2 = 'id>1,record>100'; $param3 = array( array('count' => 4, 'openid' => '456', 'record' => '500') ); $param4 = array('count' => 4, 'openid' => '456', 'record' => '500'); //dump($db->where('id=4')->save($param4)); dump($db->where('count=4')->delete()); die; dump($db->data($param3)->add()); dump(array_keys($param3)); dump(array_values($param3)); dump(implode(',', array_values($param3))); dump(implode(',', array_keys($param3))); dump($db->where($param2)->order('id')->limit(2)->find()); dump($db->options_handle($param1)); dump($db->options_handle($param2)); dump($db->options_handle($param3)); $array = array('id', 'count'); $num = '123'; if (is_string($num)) { echo 'true'; } dump($db->getField($array)); dump($db->select_table_fields($table)); die; // $table = 'zyd_fuweng_user'; //选择指定的数据库,并返回其中全部信息 $table_msg = $db->select_table($table); //选择指定数据库,返回数据库的字段信息 $table_field_msg = $db->select_table_fields($table); //条件搜索,传入条件均为数据 $where = array( 'id' => 1 ); $data = array( 'headimgurl' => 'helen.jpg' ); dump($db->where($where)->field('field')); dump($table);