zoukankan      html  css  js  c++  java
  • 戳到爆的DB类。。只为记录自己的历程。PHP

      1 <?php
      2     class Db{
      3         private $_link    = '';      //设置连接信息
      4         private $_db_host = '';      //数据库服务器主机
      5         private $_db_user = '';      //数据库用户
      6         private $_db_pwd  = '';      //数据库密码
      7         private $_db_name = '';      //所使用数据库的名字
      8         private $_db_charset = '';   //设置字符集
      9         private $_query   = '';    //当前sql指令
     10         private $_table_name  = '';  //设置数据表
     11         private $_table_prefix = ''; //数据表的前缀
     12         private $_where   = '';       //设置where条件
     13         private $_order   = '';      //设置排序条件
     14         private $_field   = '';      //设置选择区域
     15         private $_limit   = '';      //设置limit条件
     16         private $_result  = '';      //需要返回的结果
     17         private $_fields  = '';      //设置操作内容
     18 
     19         function __construct($info_data){
     20             if(!is_array($info_data)) exit('请以关联数据的形式设置实例化Db');
     21             $this->_db_host = $info_data['db_host'];
     22             $this->_db_name = $info_data['db_name'];
     23             $this->_db_user = $info_data['db_user'];
     24             $this->_db_pwd  = $info_data['db_pwd'];
     25             $this->_db_charset = $info_data['db_charset'];
     26             $this->_table_prefix = $info_data['table_prefix'];
     27             //数据库连接
     28             $this->_link = @mysql_connect($this->_db_host,$this->_db_user,$this->_db_pwd) or die('数据库连接失败');
     29             //设置活动的数据库
     30             @mysql_select_db($this->_db_name);
     31             //设置输出字符集
     32             @mysql_query('set names ' . $this->_db_charset);
     33             return $this;
     34         }
     35 
     36         /*设置需要操作的tableName*/
     37         public function tablename($tableName){
     38             if(!empty($tableName)){
     39                 $this->_table_name = $tableName;
     40             }
     41             return $this;
     42         }
     43 
     44         /*设置where*/
     45         public function where($where = ''){
     46             if(!empty($where)){
     47                 $this->_where = ' where ' . $where;
     48             }
     49             return $this;
     50         }
     51 
     52         /*设置排序*/
     53         public function order($order = ''){
     54             if(!empty($order)){
     55                 $this->_order = ' order by '.$order;
     56             }
     57             return $this;
     58         }
     59 
     60         /*设置field条件*/
     61         public function field($field = ''){
     62             if(!empty($field)){
     63                 //进行重复过滤
     64                 $opeField = array_unique(explode(',', $field));
     65                 $field = ' `' . implode('`,`', $opeField) . '` ';
     66                 $this->_fields = $field;
     67             }else{
     68                 $this->_fields = ' * ';
     69             }
     70             return $this;
     71         }
     72 
     73         /*设置limit条件*/
     74         public function limit($limit = ''){
     75             if(!empty($limit)){
     76                 if(is_numeric($limit)){
     77                     $this->_limit = ' limit 0,' . $limit . ' ';
     78                 }else{
     79                     $this->_limit = ' limit ' . $limit;  
     80                 }
     81             }
     82             return $this;
     83         }
     84 
     85         /*增加*/
     86         public function add($data){
     87             if(!empty($data)){
     88                 $str = ' set ';
     89                 foreach ($data as $key => $value) {
     90                     $str .= "$key=$value,";
     91                 }
     92                 $str = rtrim($str,',');
     93                 $this->_query = mysql_query("insert into ".$this->_table_prefix.$this->_table_name.$str);
     94                 if($this->_query){
     95                     return true;
     96                 }else{
     97                     return false;
     98                 }
     99             }
    100         }
    101 
    102         /*find查找一条数据*/
    103         public function find(){
    104             $fields = empty($fields)? ' * ' : $this->_fields;
    105             $this->_query = mysql_query('select '.$fields.' from '.$this->_table_prefix.$this->_table_name.$this->_where.$this->_order.$this->_limit);
    106             if(is_resource($this->_query)){
    107                 $result = mysql_fetch_assoc($this->_query);
    108                 if(!empty($result)){
    109                     return $result;
    110                 }
    111             }
    112             //释放内存
    113             mysql_free_result($this->_query);
    114         }
    115 
    116         /*findAll查找多行数据*/
    117         public function findAll(){
    118             $fields = empty($fields)? ' * ' : $this->_fields;
    119             $this->_query = mysql_query('select'.$fields.' from '.$this->_table_prefix.$this->_table_name.$this->_where.$this->_order.$this->_limit);
    120             if(is_resource($this->_query)){
    121                 $resultData = array();
    122                 while($result = mysql_fetch_assoc($this->_query)){
    123                     $resultData[] = $result;
    124                 }
    125                 return $resultData;
    126             }
    127             //释放内存
    128             mysql_free_result($this->_query);
    129         }
    130 
    131         /*select*/
    132         public function select(){
    133             $fields = empty($fields)? ' * ' : $this->_fields;
    134             $this->_query = mysql_query('select'.$fields.' from '.$this->_table_prefix.$this->_table_name.$this->_where.$this->_order.$this->_limit);
    135             if(is_resource($this->_query)){
    136                 $resultData = array();
    137                 while($result = mysql_fetch_assoc($this->_query)){
    138                     $resultData[] = $result;
    139                 }
    140                 return $resultData;
    141             }
    142             //释放内存
    143             mysql_free_result($this->_query);
    144         }
    145 
    146         /*根据where中的条件进行删除*/
    147         public function delete(){
    148             $this->_query = mysql_query("delete from ".$this->_table_prefix.$this->_table_name.$this->_where);
    149             if($this->_query){
    150                 return true;
    151             }else{
    152                 return false;
    153             }
    154             mysql_free_result($this->_query);
    155         }
    156 
    157         /*根据where中的条件来进行更新*/
    158         public function update($data){
    159             if(!empty($data)){
    160                 $str = 'update `'.$this->_table_prefix.$this->_table_name.'` set ';
    161                 foreach ($data as $key => $value) {
    162                      $str .= "`$key`='".$value."',";
    163                 } 
    164                 $str = rtrim($str,',');
    165                 $str .= '  '.$this->_where;
    166                 $this->_query = mysql_query($str);
    167                 if($this->_query){
    168                     return true;
    169                 }else{
    170                     return false;
    171                 }
    172             }
    173             mysql_free_result($this->_query);
    174         }
    175 
    176         /*关闭数据库*/
    177         function __destruct(){
    178             mysql_close($this->_link);
    179         }
    180     }
    181 
    182 ?>
  • 相关阅读:
    7 Django的模板层
    6 Django的视图层
    5 Django-1的路由层(URLconf)
    qt 如何安装 Debuggers 调试器 ?
    window7 x64 vs2015 如何编译 libqr 二维码生成库?
    如何在 window7 环境编译 zlib 库?
    LNK2026 模块对于 SAFESEH 映像是不安全的
    如何识别二维码?
    qt Multimedia 模块类如何使用?
    qt 使用msvc编译器出现乱码如何解决?字符串中存在空格?
  • 原文地址:https://www.cnblogs.com/zafuacm/p/4074602.html
Copyright © 2011-2022 走看看