Heim > php教程 > PHP源码 > Hauptteil

php-mysql的封装类

PHP中文网
Freigeben: 2016-05-26 08:18:56
Original
1120 Leute haben es durchsucht

<?php
/**
* Desc: php操作mysql的封装类
* Author zhifeng
* Date: 2015/04/15
* 连接模式:PDO
*/
class MMysql {
protected static $_dbh = null; //静态属性,所有数据库实例共用,避免重复连接数据库
   protected $_dbType = &#39;mysql&#39;;
protected $_pconnect = true; //是否使用长连接
   protected $_host = &#39;localhost&#39;;
protected $_port = 3306;
   protected $_user = &#39;root&#39;;
   protected $_pass = &#39;root&#39;;
   protected $_dbName = null; //数据库名
protected $_sql = false; //最后一条sql语句
   protected $_where = &#39;&#39;;
   protected $_order = &#39;&#39;;
   protected $_limit = &#39;&#39;;
   protected $_field = &#39;*&#39;;
   protected $_clear = 0; //状态,0表示查询条件干净,1表示查询条件污染
   protected $_trans = 0; //事务指令数 
   /**
    * 初始化类
    * @param array $conf 数据库配置
    */
   public function __construct(array $conf) {
class_exists(&#39;PDO&#39;) or die("PDO: class not exists.");
$this->_host = $conf[&#39;host&#39;];
$this->_port = $conf[&#39;port&#39;];
$this->_user = $conf[&#39;user&#39;];
$this->_pass = $conf[&#39;passwd&#39;];
$this->_dbName = $conf[&#39;dbname&#39;];
//连接数据库
       if ( is_null(self::$_dbh) ) {
           $this->_connect();
       }
   }
/**
    * 连接数据库的方法
    */
protected function _connect() {
$dsn = $this->_dbType.&#39;:host=&#39;.$this->_host.&#39;;port=&#39;.$this->_port.&#39;;dbname=&#39;.$this->_dbName;
$options = $this->_pconnect ? array(PDO::ATTR_PERSISTENT=>true) : array();
try { 
$dbh = new PDO($dsn, $this->_user, $this->_pass, $options);
           $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);  //设置如果sql语句执行错误则抛出异常,事务会自动回滚
           $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); //禁用prepared statements的仿真效果(防SQL注入)
} catch (PDOException $e) { 
           die(&#39;Connection failed: &#39; . $e->getMessage());
}
$dbh->exec(&#39;SET NAMES utf8&#39;);
self::$_dbh = $dbh;
}
   /** 
   * 字段和表名添加 `符号
   * 保证指令中使用关键字不出错 针对mysql 
   * @param string $value 
   * @return string 
   */ 
   protected function _addChar($value) { 
       if (&#39;*&#39;==$value || false!==strpos($value,&#39;(&#39;) || false!==strpos($value,&#39;.&#39;) || false!==strpos($value,&#39;`&#39;)) { 
           //如果包含* 或者 使用了sql方法 则不作处理 
       } elseif (false === strpos($value,&#39;`&#39;) ) { 
           $value = &#39;`&#39;.trim($value).&#39;`&#39;;
       } 
       return $value; 
   }
   /** 
   * 取得数据表的字段信息 
   * @param string $tbName 表名
   * @return array 
   */ 
   protected function _tbFields($tbName) {
       $sql = &#39;SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME="&#39;.$tbName.&#39;" AND TABLE_SCHEMA="&#39;.$this->_dbName.&#39;"&#39;;
       $stmt = self::$_dbh->prepare($sql);
       $stmt->execute();
       $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
       $ret = array();
       foreach ($result as $key=>$value) {
           $ret[$value[&#39;COLUMN_NAME&#39;]] = 1;
       }
       return $ret;
   }
   /** 
   * 过滤并格式化数据表字段
   * @param string $tbName 数据表名 
   * @param array $data POST提交数据 
   * @return array $newdata 
   */
   protected function _dataFormat($tbName,$data) {
       if (!is_array($data)) return array();
       $table_column = $this->_tbFields($tbName);
       $ret=array();
       foreach ($data as $key=>$val) {
           if (!is_scalar($val)) continue; //值不是标量则跳过
           if (array_key_exists($key,$table_column)) {
               $key = $this->_addChar($key);
               if (is_int($val)) { 
                   $val = intval($val); 
               } elseif (is_float($val)) { 
                   $val = floatval($val); 
               } elseif (preg_match(&#39;/^\(\w*(\+|\-|\*|\/)?\w*\)$/i&#39;, $val)) {
                   // 支持在字段的值里面直接使用其它字段 ,例如 (score+1) (name) 必须包含括号
                   $val = $val;
               } elseif (is_string($val)) { 
                   $val = &#39;"&#39;.addslashes($val).&#39;"&#39;;
               }
               $ret[$key] = $val;
           }
       }
       return $ret;
   }
   
   /**
   * 执行查询 主要针对 SELECT, SHOW 等指令
   * @param string $sql sql指令 
   * @return mixed 
   */ 
   protected function _doQuery($sql=&#39;&#39;) {
       $this->_sql = $sql;
       $pdostmt = self::$_dbh->prepare($this->_sql); //prepare或者query 返回一个PDOStatement
       $pdostmt->execute();
       $result = $pdostmt->fetchAll(PDO::FETCH_ASSOC);
       return $result;
   }
   
   /** 
   * 执行语句 针对 INSERT, UPDATE 以及DELETE,exec结果返回受影响的行数
   * @param string $sql sql指令 
   * @return integer 
   */ 
   protected function _doExec($sql=&#39;&#39;) {
       $this->_sql = $sql;
       return self::$_dbh->exec($this->_sql);
   }
   /** 
   * 执行sql语句,自动判断进行查询或者执行操作 
   * @param string $sql SQL指令 
   * @return mixed 
   */ 
   public function doSql($sql=&#39;&#39;) {
       $queryIps = &#39;INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|LOAD DATA|SELECT .* INTO|COPY|ALTER|GRANT|REVOKE|LOCK|UNLOCK&#39;; 
       if (preg_match(&#39;/^\s*"?(&#39; . $queryIps . &#39;)\s+/i&#39;, $sql)) { 
           return $this->_doExec($sql);
       }
       else {
           //查询操作
           return $this->_doQuery($sql);
       }
   }
   /** 
   * 获取最近一次查询的sql语句 
   * @return String 执行的SQL 
   */ 
   public function getLastSql() { 
       return $this->_sql;
   }
   /**
    * 插入方法
    * @param string $tbName 操作的数据表名
    * @param array $data 字段-值的一维数组
    * @return int 受影响的行数
    */
   public function insert($tbName,array $data){
       $data = $this->_dataFormat($tbName,$data);
       if (!$data) return;
       $sql = "insert into ".$tbName."(".implode(&#39;,&#39;,array_keys($data)).") values(".implode(&#39;,&#39;,array_values($data)).")";
return $this->_doExec($sql);
   }
   /**
    * 删除方法
    * @param string $tbName 操作的数据表名
    * @return int 受影响的行数
    */
   public function delete($tbName) {
       //安全考虑,阻止全表删除
       if (!trim($this->_where)) return false;
       $sql = "delete from ".$tbName." ".$this->_where;
       $this->_clear = 1;
       $this->_clear();
       return $this->_doExec($sql);
   }
   /**
    * 更新函数
    * @param string $tbName 操作的数据表名
    * @param array $data 参数数组
    * @return int 受影响的行数
    */
   public function update($tbName,array $data) {
       //安全考虑,阻止全表更新
       if (!trim($this->_where)) return false;
       $data = $this->_dataFormat($tbName,$data);
       if (!$data) return;
       $valArr = &#39;&#39;;
       foreach($data as $k=>$v){
           $valArr[] = $k.&#39;=&#39;.$v;
       }
       $valStr = implode(&#39;,&#39;, $valArr);
       $sql = "update ".trim($tbName)." set ".trim($valStr)." ".trim($this->_where);
       return $this->_doExec($sql);
   }
   /**
    * 查询函数
    * @param string $tbName 操作的数据表名
    * @return array 结果集
    */
   public function select($tbName=&#39;&#39;) {
       $sql = "select ".trim($this->_field)." from ".$tbName." ".trim($this->_where)." ".trim($this->_order)." ".trim($this->_limit);
       $this->_clear = 1;
       $this->_clear();
       return $this->_doQuery(trim($sql));
   }
   /**
    * @param mixed $option 组合条件的二维数组,例:$option[&#39;field1&#39;] = array(1,&#39;=>&#39;,&#39;or&#39;)
    * @return $this
    */
   public function where($option) {
       if ($this->_clear>0) $this->_clear();
       $this->_where = &#39; where &#39;;
       $logic = &#39;and&#39;;
       if (is_string($option)) {
           $this->_where .= $option;
       }
       elseif (is_array($option)) {
           foreach($option as $k=>$v) {
               if (is_array($v)) {
                   $relative = isset($v[1]) ? $v[1] : &#39;=&#39;;
                   $logic    = isset($v[2]) ? $v[2] : &#39;and&#39;;
                   $condition = &#39; (&#39;.$this->_addChar($k).&#39; &#39;.$relative.&#39; &#39;.$v[0].&#39;) &#39;;
               }
               else {
                   $logic = &#39;and&#39;;
                   $condition = &#39; (&#39;.$this->_addChar($k).&#39;=&#39;.$v.&#39;) &#39;;
               }
               $this->_where .= isset($mark) ? $logic.$condition : $condition;
               $mark = 1;
           }
       }
       return $this;
   }
   /**
    * 设置排序
    * @param mixed $option 排序条件数组 例:array(&#39;sort&#39;=>&#39;desc&#39;)
    * @return $this
    */
   public function order($option) {
       if ($this->_clear>0) $this->_clear();
       $this->_order = &#39; order by &#39;;
       if (is_string($option)) {
           $this->_order .= $option;
       }
       elseif (is_array($option)) {
           foreach($option as $k=>$v){
               $order = $this->_addChar($k).&#39; &#39;.$v;
               $this->_order .= isset($mark) ? &#39;,&#39;.$order : $order;
               $mark = 1;
           }
       }
       return $this;
   }
   /**
    * 设置查询行数及页数
    * @param int $page pageSize不为空时为页数,否则为行数
    * @param int $pageSize 为空则函数设定取出行数,不为空则设定取出行数及页数
    * @return $this
    */
   public function limit($page,$pageSize=null) {
       if ($this->_clear>0) $this->_clear();
       if ($pageSize===null) {
           $this->_limit = "limit ".$page;
       }
       else {
           $pageval = intval( ($page - 1) * $pageSize);
           $this->_limit = "limit ".$pageval.",".$pageSize;
       }
       return $this;
   }
   /**
    * 设置查询字段
    * @param mixed $field 字段数组
    * @return $this
    */
   public function field($field){
       if ($this->_clear>0) $this->_clear();
       if (is_string($field)) {
           $field = explode(&#39;,&#39;, $field);
       }
       $nField = array_map(array($this,&#39;_addChar&#39;), $field);
       $this->_field = implode(&#39;,&#39;, $nField);
       return $this;
   }
   /**
    * 清理标记函数
    */
   protected function _clear() {
       $this->_where = &#39;&#39;;
       $this->_order = &#39;&#39;;
       $this->_limit = &#39;&#39;;
       $this->_field = &#39;*&#39;;
       $this->_clear = 0;
   }
   /**
    * 手动清理标记
    * @return $this
    */
   public function clearKey() {
       $this->_clear();
       return $this;
   }
   /**
   * 启动事务 
   * @return void 
   */ 
   public function startTrans() { 
       //数据rollback 支持 
       if ($this->_trans==0) self::$_dbh->beginTransaction();
       $this->_trans++; 
       return; 
   }
   
   /** 
   * 用于非自动提交状态下面的查询提交 
   * @return boolen 
   */
   public function commit() {
       $result = true;
       if ($this->_trans>0) { 
           $result = self::$_dbh->commit(); 
           $this->_trans = 0;
       } 
       return $result;
   }
   /** 
   * 事务回滚 
   * @return boolen 
   */ 
   public function rollback() {
       $result = true;
       if ($this->_trans>0) {
           $result = self::$_dbh->rollback();
           $this->_trans = 0;
       }
       return $result;
   }
   /**
   * 关闭连接
   * PHP 在脚本结束时会自动关闭连接。
   */
   public function close() {
       if (!is_null(self::$_dbh)) self::$_dbh = null;
   }
}
Nach dem Login kopieren

2. 例子

<?php
$mysql = new MMysql($configArr);
//插入
$data = array(
&#39;sid&#39;=>101,
&#39;aa&#39;=>123456,
&#39;bbc&#39;=>&#39;aaaaaaaaaaaaaa&#39;,
);
$mysql->insert(&#39;t_table&#39;,$data);
//查询
$res = $mysql->field(array(&#39;sid&#39;,&#39;aa&#39;,&#39;bbc&#39;))
->order(array(&#39;sid&#39;=>&#39;desc&#39;,&#39;aa&#39;=>&#39;asc&#39;))
->where(array(&#39;sid&#39;=>"101",&#39;aa&#39;=>array(&#39;123455&#39;,&#39;>&#39;,&#39;or&#39;)))
->limit(1,2)
->select(&#39;t_table&#39;);
$res = $mysql->field(&#39;sid,aa,bbc&#39;)
->order(&#39;sid desc,aa asc&#39;)
->where(&#39;sid=101 or aa>123455&#39;)
->limit(1,2)
->select(&#39;t_table&#39;);
//获取最后执行的sql语句
$sql = $mysql->getLastSql();
//直接执行sql语句
$sql = "show tables";
$res = $mysql->doSql($sql);
//事务
$mysql->startTrans();
$mysql->where(array(&#39;sid&#39;=>102))->update(&#39;t_table&#39;,array(&#39;aa&#39;=>666666));
$mysql->where(array(&#39;sid&#39;=>103))->update(&#39;t_table&#39;,array(&#39;bbc&#39;=>&#39;呵呵8888呵呵&#39;));
$mysql->where(array(&#39;sid&#39;=>104))->delete(&#39;t_table&#39;);
$mysql->commit();
Nach dem Login kopieren

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage