php基于PDO实现功能强大的MYSQL封装类实例


Posted in PHP onFebruary 27, 2017

本文实例讲述了php基于PDO实现功能强大的MYSQL封装类。分享给大家供大家参考,具体如下:

class CPdo{
 protected $_dsn = "mysql:host=localhost;dbname=test";
 protected $_name = "root";
 protected $_pass = "";
 protected $_condition = array();
 protected $pdo;
 protected $fetchAll;
 protected $query;
 protected $result;
 protected $num;
 protected $mode;
 protected $prepare;
 protected $row;
 protected $fetchAction;
 protected $beginTransaction;
 protected $rollback;
 protected $commit;
 protected $char;
 private static $get_mode;
 private static $get_fetch_action;
 /**
 *pdo construct
 */
 public function __construct($pconnect = false) {
  $this->_condition = array(PDO::ATTR_PERSISTENT => $pconnect);
  $this->pdo_connect();
 }
 /**
 *pdo connect
 */
 private function pdo_connect() {
  try{
   $this->pdo = new PDO($this->_dsn,$this->_name,$this->_pass,$this->_condition);
  }
  catch(Exception $e) {
   return $this->setExceptionError($e->getMessage(), $e->getline, $e->getFile);
  }
 }
 /**
 *self sql get value action
 */
 public function getValueBySelfCreateSql($sql, $fetchAction = "assoc",$mode = null) {
  $this->fetchAction = $this->fetchAction($fetchAction);
  $this->result = $this->setAttribute($sql, $this->fetchAction, $mode);
  $this->AllValue = $this->result->fetchAll();
  return $this->AllValue;
 }
 /**
 *select condition can query
 */
 private function setAttribute($sql, $fetchAction, $mode) {
  $this->mode = self::getMode($mode);
  $this->fetchAction = self::fetchAction($fetchAction);
  $this->pdo->setAttribute(PDO::ATTR_CASE, $this->mode);
  $this->query = $this->base_query($sql);
  $this->query->setFetchMode($this->fetchAction);
  return $this->query;
 }
 /**
 *get mode action
 */
 private static function getMode($get_style){
  switch($get_style) {
   case null:
    self::$get_mode = PDO::CASE_NATURAL;
   break;
   case true:
    self::$get_mode = PDO::CASE_UPPER;
   break;
   case false;
   self::$get_mode= PDO::CASE_LOWER;
   break;
  }
  return self::$get_mode;
 }
 /**
 *fetch value action
 */
 private static function fetchAction($fetchAction) {
  switch($fetchAction) {
   case "assoc":
    self::$get_fetch_action = PDO::FETCH_ASSOC; //asso array
   break;
   case "num":
    self::$get_fetch_action = PDO::FETCH_NUM; //num array
   break;
   case "object":
    self::$get_fetch_action = PDO::FETCH_OBJ; //object array
   break;
   case "both":
    self::$get_fetch_action = PDO::FETCH_BOTH; //assoc array and num array
   break;
   default:
    self::$get_fetch_action = PDO::FETCH_ASSOC;
   break;
  }
  return self::$get_fetch_action;
 }
 /**
 *get total num action
 */
 public function rowCount($sql) {
  $this->result = $this->base_query($sql);
  $this->num = $this->result->rowCount();
  return $this->num;
 }
 /*
 *simple query and easy query action
 */
 public function query($table, $column = "*",$condition = array(), $group = "",$order = "", $having = "", $startSet = "",$endSet = "",$fetchAction = "assoc",$params = null){
  $sql = "select ".$column." from `".$table."` ";
  if ($condition != null) {
   foreach($condition as $key=>$value) {
    $where .= "$key = '$value' and ";
   }
   $sql .= "where $where";
   $sql .= "1 = 1 ";
  }
  if ($group != "") {
   $sql .= "group by ".$group." ";
  }
  if ($order != "") {
   $sql .= " order by ".$order." ";
  }
  if ($having != "") {
   $sql .= "having '$having' ";
  }
  if ($startSet != "" && $endSet != "" && is_numeric($endSet) && is_numeric($startSet)) {
   $sql .= "limit $startSet,$endSet";
  }
  $this->result = $this->getValueBySelfCreateSql($sql, $fetchAction, $params);
  return $this->result;
 }
 /**
 *execute delete update insert and so on action
 */
 public function exec($sql) {
  $this->result = $this->pdo->exec($sql);
  $substr = substr($sql, 0 ,6);
  if ($this->result) {
   return $this->successful($substr);
  } else {
   return $this->fail($substr);
  }
 }
 /**
 *prepare action
 */
 public function prepare($sql) {
  $this->prepare = $this->pdo->prepare($sql);
  $this->setChars();
  $this->prepare->execute();
  while($this->rowz = $this->prepare->fetch()) {
   return $this->row;
  }
 }
 /**
 *USE transaction
 */
 public function transaction($sql) {
  $this->begin();
  $this->result = $this->pdo->exec($sql);
  if ($this->result) {
   $this->commit();
  } else {
   $this->rollback();
  }
 }
 /**
 *start transaction
 */
 private function begin() {
  $this->beginTransaction = $this->pdo->beginTransaction();
  return $this->beginTransaction;
 }
 /**
 *commit transaction
 */
 private function commit() {
  $this->commit = $this->pdo->commit();
  return $this->commit;
 }
 /**
 *rollback transaction
 */
 private function rollback() {
  $this->rollback = $this->pdo->rollback();
  return $this->rollback;
 }
 /**
 *base query
 */
 private function base_query($sql) {
  $this->setChars();
  $this->query = $this->pdo->query($sql);
  return $this->query;
 }
 /**
 *set chars
 */
 private function setChars() {
  $this->char = $this->pdo->query("SET NAMES 'UTF8'");
  return $this->char;
 }
 /**
 *process sucessful action 
 */
 private function successful($params){
  return "The ".$params." action is successful";
 }
 /**
 *process fail action
 */
 private function fail($params){
  return "The ".$params." action is fail";
 }
 /**
 *process exception action
 */
 private function setExceptionError($getMessage, $getLine ,$getFile) {
  echo "Error message is ".$getMessage."<br /> The Error in ".$getLine." line <br /> This file dir on ".$getFile;
  exit();
 }
}

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

PHP 相关文章推荐
自动分页的不完整解决方案
Jan 12 PHP
PHP之生成GIF动画的实现方法
Jun 07 PHP
PHP中spl_autoload_register函数的用法总结
Nov 07 PHP
php中使用PHPExcel读写excel(xls)文件的方法
Sep 15 PHP
php使用fopen创建utf8编码文件的方法
Oct 31 PHP
PHP易混淆函数的区别及用法汇总
Nov 22 PHP
PHP魔术方法__GET、__SET使用实例
Nov 25 PHP
php图片的二进制转换实现方法
Dec 15 PHP
PHP连接SQLServer2005的方法
Jan 27 PHP
php使用curl打开https网站的方法
Jun 17 PHP
thinkphp微信开之安全模式消息加密解密不成功的解决办法
Dec 02 PHP
Laravel 模型使用软删除-左连接查询-表起别名示例
Oct 24 PHP
php实现通过soap调用.Net的WebService asmx文件
Feb 27 #PHP
PHP 中使用ajax时一些常见错误总结整理
Feb 27 #PHP
PHP/HTML混写的四种方式总结
Feb 27 #PHP
老生常谈文本文件和二进制文件的区别
Feb 27 #PHP
php实现数据库的增删改查
Feb 26 #PHP
php查询及多条件查询
Feb 26 #PHP
php批量删除操作代码分享
Feb 26 #PHP
You might like
php header示例代码(推荐)
2010/09/08 PHP
Laravel中扩展Memcached缓存驱动实现使用阿里云OCS缓存
2015/02/10 PHP
解决更换PHP5.4以上版本后Dedecms后台登录空白问题的方法
2015/10/23 PHP
Laravel如何使用Redis共享Session
2018/02/23 PHP
使用prototype.js进行异步操作
2007/02/07 Javascript
jQuery的实现原理的模拟代码 -4 重要的扩展函数 extend
2010/08/03 Javascript
解析John Resig Simple JavaScript Inheritance代码
2012/12/03 Javascript
通过javascript获取iframe里的值示例代码
2013/06/24 Javascript
jQuery的attr与prop使用介绍
2013/10/10 Javascript
jquery操作angularjs对象
2015/06/26 Javascript
js实现具有高亮显示效果的多级菜单代码
2015/09/01 Javascript
javascript实现保留两位小数的多种方法
2015/12/18 Javascript
JQuery ztree带筛选、异步加载实例讲解
2016/02/25 Javascript
jQuery ajax方法传递中文时出现中文乱码的解决方法
2016/07/25 Javascript
bootstrap警告框使用方法解析
2017/01/13 Javascript
Javascript 两种刷新方法以及区别和适用范围
2017/01/17 Javascript
javascript 正则表达式去空行方法
2017/01/24 Javascript
Vue中添加过渡效果的方法
2017/03/16 Javascript
Vue 单文件中的数据传递示例
2017/03/21 Javascript
js canvas实现二维码和图片合成的海报
2020/11/19 Javascript
Vue组件化开发之通用型弹出框的实现
2020/02/28 Javascript
JS apply用法总结和使用场景实例分析
2020/03/14 Javascript
对python中的float除法和整除法的实例详解
2019/07/20 Python
提升Python效率之使用循环机制代替递归函数
2019/07/23 Python
django-初始配置(纯手写)详解
2019/07/30 Python
django与vue的完美结合_实现前后端的分离开发之后在整合的方法
2019/08/12 Python
Django框架 Pagination分页实现代码实例
2019/09/04 Python
简单了解python元组tuple相关原理
2019/12/02 Python
基于HTML5超酷摄像头(HTML5 webcam)拍照功能实现代码
2012/12/13 HTML / CSS
介绍一下Mysql的存储引擎
2015/02/12 面试题
金融专业毕业生推荐信
2013/11/26 职场文书
酒店财务总监岗位职责
2015/04/03 职场文书
物业工程部主管岗位职责
2015/04/16 职场文书
史上最牛辞职信
2015/05/13 职场文书
2015年机关党建工作总结
2015/05/22 职场文书
廉政党课工作报告案例
2019/06/21 职场文书