123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- <?php
- header("Content-Type:text/html;charset=utf-8");
-
- /**
- *php操作mysql的工具类
- */
- class Db{
- private $_db = null;//数据库连接句柄
- private $_table = null;//表名
- private $_where = null;//where条件
- private $_order = null;//order排序
- private $_limit = null;//limit限定查询
- private $_group = null;//group分组
- private $_configs = array(
- 'hostname' => 'localhost',
- 'dbname' => 'test',
- 'username' => 'root',
- 'password' => '1234'
- );//数据库配置
-
- /**
- * 构造函数,连接数据库
- */
- public function __construct($config = array()){
- $link = $this->_db;
- if (!empty($config)) $this->_configs = $config;
-
- if(!$link){
- $db = mysqli_connect($this->_configs['hostname'],$this->_configs['username'],$this->_configs['password'],$this->_configs['dbname']);
- mysqli_query($db,"set names utf8");
- if(!$db){
- $this->ShowException("错误信息".mysqli_connect_error());
- }
- $this->_db = $db;
- }
- }
-
- /**
- * 获取所有数据
- *
- * @param <type> $table The table
- *
- * @return boolean All.
- */
- public function getAll($table=null){
- $link = $this->_db;
- if(!$link)return false;
- $sql = "SELECT * FROM {$table}";
- $data = mysqli_fetch_all($this->execute($sql));
- return $data;
- }
-
- public function table($table){
- $this->_table = $table;
- return $this;
- }
-
- /**
- * 实现查询操作
- *
- * @param string $fields The fields
- *
- * @return boolean ( description_of_the_return_value )
- */
- public function select($fields="*"){
- $fieldsStr = '';
- $link = $this->_db;
- if(!$link)return false;
- if(is_array($fields)){
- $fieldsStr = implode(',', $fields);
- }elseif(is_string($fields)&&!empty($fields)){
- $fieldsStr = $fields;
- }
- $sql = "SELECT {$fields} FROM {$this->_table} {$this->_where} {$this->_order} {$this->_limit}";
- $res = $this->execute($sql);
- $list = array();
- while($row = mysqli_fetch_assoc($res)){
- $list[] = $row;
- }
- return $list;
- }
-
- /**
- * order排序
- *
- * @param string $order The order
- *
- * @return boolean ( description_of_the_return_value )
- */
- public function order($order=''){
- $orderStr = '';
- $link = $this->_db;
- if(!$link)return false;
- if(is_string($order)&&!empty($order)){
- $orderStr = "ORDER BY ".$order;
- }
- $this->_order = $orderStr;
- return $this;
- }
-
- /**
- * where条件
- *
- * @param string $where The where
- *
- * @return <type> ( description_of_the_return_value )
- */
- public function where($where=''){
- $whereStr = '';
- $link = $this->_db;
- if(!$link)return $link;
- if(is_array($where)){
- foreach ($where as $key => $value) {
- $whereArr[] = "`".$key."` = '".$value."'";
- }
- $whereStr = implode(' AND ', $whereArr);
- $whereStr = "WHERE ".$whereStr;
- }elseif(is_string($where)&&!empty($where)){
- $whereStr = "WHERE ".$where;
- }
- $this->_where = $whereStr;
- return $this;
- }
-
- /**
- * group分组
- *
- * @param string $group The group
- *
- * @return boolean ( description_of_the_return_value )
- */
- public function group($group=''){
- $groupStr = '';
- $link = $this->_db;
- if(!$link)return false;
- if(is_array($group)){
- $groupStr = "GROUP BY ".implode(',',$group);
- }elseif(is_string($group)&&!empty($group)){
- $groupStr = "GROUP BY ".$group;
- }
- $this->_group = $groupStr;
- return $this;
- }
-
- /**
- * limit限定查询
- *
- * @param string $limit The limit
- *
- * @return <type> ( description_of_the_return_value )
- */
- public function limit($limit=''){
- $limitStr = '';
- $link = $this->_db;
- if(!$link)return $link;
- if(is_string($limit)||!empty($limit)){
- $limitStr = "LIMIT ".$limit;
- }elseif(is_numeric($limit)){
- $limitStr = "LIMIT ".$limit;
- }
- $this->_limit = $limitStr;
- return $this;
- }
-
- /**
- * 执行sql语句
- *
- * @param <type> $sql The sql
- *
- * @return boolean ( description_of_the_return_value )
- */
- public function query($sql=null){
- $link = $this->_db;
- if(!$link)return false;
- $res = mysqli_query($this->_db,$sql);
- if(!$res){
- $errors = mysqli_error_list($this->_db);
- // $this->ShowException("报错啦!<br/>错误号:".$errors[0]['errno']."<br/>sql:".$sql."<br/>SQL错误状态:".$errors[0]['sqlstate']."<br/>错误信息:".$errors[0]['error']);
- die();
- }
- $list = array();
- while($row = mysqli_fetch_assoc($res)){
- $list[] = $row;
- }
- return $list;
- }
- /**
- * 执行sql语句
- *
- * @param <type> $sql The sql
- *
- * @return boolean ( description_of_the_return_value )
- */
- public function execute($sql=null){
- $link = $this->_db;
- if(!$link)return false;
- $res = mysqli_query($this->_db,$sql);
- if(!$res){
- $errors = mysqli_error_list($this->_db);
- // $this->ShowException("报错啦!<br/>错误号:".$errors[0]['errno']."<br/>sql:".$sql."<br/>SQL错误状态:".$errors[0]['sqlstate']."<br/>错误信息:".$errors[0]['error']);
- die();
- }
- $this->_table = null;//表名
- $this->_where = null;//where条件
- $this->_order = null;//order排序
- $this->_limit = null;//limit限定查询
- $this->_group = null;//group分组
- return $res;
- }
-
- /**
- * 插入数据
- *
- * @param <type> $data The data
- *
- * @return boolean ( description_of_the_return_value )
- */
- public function insert($data){
- $link = $this->_db;
- if(!$link)return false;
- if(is_array($data)){
- $keys = '';
- $values = '';
- foreach ($data as $key => $value) {
- $keys .= "`".$key."`,";
- $values .= "'".$value."',";
- }
- $keys = rtrim($keys,',');
- $values = rtrim($values,',');
- }
- $sql = "INSERT INTO `{$this->_table}`({$keys}) VALUES({$values})";
- mysqli_query($this->_db,$sql);
- $insertId = mysqli_insert_id($this->_db);
- return $insertId;
- }
-
- /**
- * 更新数据
- *
- * @param <type> $data The data
- *
- * @return <type> ( description_of_the_return_value )
- */
- public function update($data){
- $link = $this->_db;
- if(!$link)return $link;
- if(is_array($data)){
- $dataStr = '';
- foreach ($data as $key => $value) {
- $dataStr .= "`".$key."`='".$value."',";
- }
- $dataStr = rtrim($dataStr,',');
- }
- $sql = "UPDATE `{$this->_table}` SET {$dataStr} {$this->_where} {$this->_order} {$this->_limit}";
- $res = $this->execute($sql);
- return $res;
- }
-
- /**
- * 删除数据
- *
- * @return <type> ( description_of_the_return_value )
- */
- public function delete(){
- $link = $this->_db;
- if(!$link)return $link;
- $sql = "DELETE FROM `{$this->_table}` {$this->_where}";
- $res = $this->execute($sql);
- return $res;
- }
-
- /**
- * 异常信息输出
- *
- * @param <type> $var The variable
- */
- private function ShowException($var){
- if(is_bool($var)){
- var_dump($var);
- }else if(is_null($var)){
- var_dump(NULL);
- }else{
- echo "<pre style='position:relative;z-index:1000;padding:10px;border-radius:5px;background:#F5F5F5;border:1px solid #aaa;font-size:14px;line-height:18px;opacity:0.9;'>".print_r($var,true)."</pre>";
- }
- }
-
- // 断开mysql连接
- public function close(){
- mysqli_close($this->_db);
- }
- }
|