首页 > 作文

PHP封装类似thinkphp连贯操作数据库Db类与简单应用示例

更新时间:2023-04-07 08:53:16 阅读: 评论:0

本文实例讲述了php封装类似thinkphp连贯操作数据库db类与简单应用。分享给大家供大家参考,具体如下:

<?phpheader("content-type:text/html;chart=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',        'urname' => 'root',        'password' => '1234'      );//数据库配置  /**   * 构造函数,连接数据库   */  public function __construct(){    $link = $this->_db天津市大学排名;    if(!$link){      $db = mysqli_connect($this->_configs['hostname'],$this->_configs['urname'],$this->_configs['password'],$this->_configs['dbname']);      mysqli_query($db,"t 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 fal;    $sql = "lect * 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 lect($fields="*"){    $fieldsstr = '';    $link = $this->_db;    if(!$link)return fal;    if(is_array($fields)){      $fieldsstr = implode(',', $fields);    }elif(is_string($fields)&&!empty($fields)){      $fieldsstr = $fields;    }    $sql = "lect {$fields} from {$this->_table} {$this->_where} {$this->_order} {$this->_limit}";    $data = mysqli_fetch_all($this->execute($sql));    return $data;  }  /**   * order排序   *   * @param   string  $order the order   *   * @return   boolean ( description_of_the_return_value )   */  public function order($order=''){    $or赵氏孤儿讲的是什么故事derstr = '';    $link = $this->_db;    if(!$link)return fal;    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) {        if($value == end($where)){          $wherestr .= "`".$key."` = '".$value."'";        }el{          $wherestr .= "`".$key."` = '".$value."' and ";        }      }      $wherestr = "where ".$wherestr;    }elif(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 fal;    if(is_array($group)){      $groupstr = "group by ".implode(',',$group);    }elif(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;    }elif(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 execute($sql=null){    $link = $this->_db;    if(!$link)return fal;    $res = mysqli_query($this->_db,$sql);    if(!$res){      $errors = mysqli_error倾情奉献_list($this->_db);      $this->showexception("报错啦!<br/>错误号:".$errors[0]['errno']."<br/潍坊护理学院>sql错误状态:".$errors[0]['sqlstate']."<br/>错误信息:".$errors[0]['error']);      die();    }    return $res;  }  /**   * 插入数据   *   * @param   <type>  $data  the data   *   * @return   boolean ( description_of_the_return_value )   */  public function inrt($data){    $link = $this->_db;    if(!$link)return fal;    if(is_array($data)){      $keys = '';      $values = '';      foreach ($data as $key => $value) {        $keys .= "`".$key."`,";        $values .= "'".$value."',";      }      $keys = rtrim($keys,',');      $values = rtrim($values,',');    }    $sql = "inrt into `{$this->_table}`({$keys}) values({$values})";    mysqli_query($this->_db,$sql);    $inrtid = mysqli_inrt_id($this->_db);    return $inrtid;  }  /**   * 更新数据   *   * @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}` t {$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);    }el if(is_null($var)){      var_dump(null);    }el{      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>";    }  }}$db = new db();//查询操作var_dump($db->table('ur')->where('id > 2')->order('id desc')->limit('2,4')->lect());//插入操作var_dump($db->table('ur')->inrt(array('urname'=>'ur','password'=>'pwd')));//更新操作var_dump($db->table('ur')->where('id = 1')->update(array('urname'=>'ur1','password'=>'pwd1')));//删除操作var_dump($db->table('ur')->where('id = 1')->delete());

更多关于php相关内容感兴趣的读者可查看本站专题:《php+mysqli数据库程序设计技巧总结》、《php面向对象程序设计入门教程》、《php数组(array)操作技巧大全》、《php基本语法入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家php程序设计有所帮助。

本文发布于:2023-04-07 08:53:14,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/86636b30ef5d0fe32614f0e7439f2f35.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:PHP封装类似thinkphp连贯操作数据库Db类与简单应用示例.doc

本文 PDF 下载地址:PHP封装类似thinkphp连贯操作数据库Db类与简单应用示例.pdf

相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图