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 相关文章推荐
PHP源代码数组统计count分析
Aug 02 PHP
如何使用PHP计算上一个月的今天
May 23 PHP
php一些错误处理的方法与技巧总结
Aug 10 PHP
phpmyadmin config.inc.php配置示例
Aug 27 PHP
PHP把空格、换行符、中文逗号等替换成英文逗号的正则表达式
May 04 PHP
PHP中strlen()和mb_strlen()的区别浅析
Jun 19 PHP
CodeIgniter中使用cookie的三种方式详解
Jul 18 PHP
最新最全PHP生成制作验证码代码详解(推荐)
Jun 12 PHP
Thinkphp微信公众号支付接口
Aug 04 PHP
php实现微信扫码自动登陆与注册功能
Sep 22 PHP
PHP iconv()函数字符编码转换的问题讲解
Mar 22 PHP
Thinkphp5.0框架视图view的模板布局用法分析
Oct 12 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
【星际争霸1】人族1v7家ZBath
2020/03/04 星际争霸
php压缩和解压缩字符串的方法
2015/03/14 PHP
php将字符串全部转换成大写或者小写的方法
2015/03/17 PHP
php断点续传之文件分割合并详解
2016/12/13 PHP
PHP常量define和const的区别详解
2019/05/18 PHP
Javascript注入技巧
2007/06/22 Javascript
JQuery AJAX提交中文乱码的解决方案
2010/07/02 Javascript
基于jquery实现图片广告轮换效果代码
2011/07/07 Javascript
jquery Ajax 实现加载数据前动画效果的示例代码
2014/02/07 Javascript
jQuery使用之处理页面元素用法实例
2015/01/19 Javascript
JS实现的随机排序功能算法示例
2017/06/09 Javascript
详解如何在react中搭建d3力导向图
2018/01/12 Javascript
如何实现一个webpack模块解析器
2018/10/24 Javascript
react koa rematch 如何打造一套服务端渲染架子
2019/06/26 Javascript
vue中 this.$set的用法详解
2019/09/06 Javascript
Nuxt.js实现一个SSR的前端博客的示例代码
2019/09/06 Javascript
用python分割TXT文件成4K的TXT文件
2009/05/23 Python
python高手之路python处理excel文件(方法汇总)
2016/01/07 Python
Python网络编程详解
2017/10/31 Python
Pycharm 设置自定义背景颜色的图文教程
2018/05/23 Python
django创建超级用户过程解析
2019/09/18 Python
python list多级排序知识点总结
2019/10/23 Python
Windows+Anaconda3+PyTorch+PyCharm的安装教程图文详解
2020/04/03 Python
python操作yaml说明
2020/04/08 Python
Python3爬虫中Splash的知识总结
2020/07/10 Python
Django Auth用户认证组件实现代码
2020/10/13 Python
python归并排序算法过程实例讲解
2020/11/04 Python
浅析HTML5 meta viewport参数
2020/10/28 HTML / CSS
爱尔兰橄榄球店:Irish Rugby Store
2019/12/05 全球购物
Java如何格式化日期
2012/08/07 面试题
食品流通安全承诺书
2014/05/22 职场文书
2015年秋季学校开学标语
2015/07/16 职场文书
Python打包为exe详细教程
2021/05/18 Python
开机音效回归! Windows 11重新引入开机铃声
2021/11/21 数码科技
动作冒险《Hell Is Us》将采用虚幻5 消灭怪物探索王国
2022/04/13 其他游戏
Android实现获取短信验证码并自动填充
2023/05/21 Java/Android