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版(5)
Oct 09 PHP
信用卡效验程序
Oct 09 PHP
PHP面向对象编程快速入门
Dec 14 PHP
php你的验证码安全码?
Jan 02 PHP
探讨:如何使用PHP实现计算两个日期间隔的年、月、周、日数
Jun 13 PHP
php模拟用户自动在qq空间发表文章的方法
Jan 07 PHP
php+mysql查询优化简单实例
Jan 13 PHP
WordPress中的shortcode短代码功能使用详解
May 17 PHP
php实现基于openssl的加密解密方法
Sep 30 PHP
Redis构建分布式锁
Mar 28 PHP
postman的安装与使用方法(模拟Get和Post请求)
Aug 06 PHP
浅析PHP中的 inet_pton 网络函数
Dec 16 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实现的遍历文件夹下所有文件,编辑删除
2010/01/05 PHP
深入理解PHP原理之错误抑制与内嵌HTML分析
2011/05/02 PHP
PHP 文件编程综合案例-文件上传的实现
2013/07/03 PHP
PHP CodeIgniter框架的工作原理研究
2015/03/30 PHP
PHP浮点比较大小的方法
2016/02/14 PHP
PHP sleep()函数, usleep()函数
2016/08/25 PHP
windows环境下使用Composer安装ThinkPHP5
2018/05/18 PHP
ASP中用Join和Array,可以加快字符连接速度的代码
2007/08/22 Javascript
使用 js+正则表达式为关键词添加链接
2014/11/11 Javascript
jQuery的context属性用法实例
2014/12/27 Javascript
jQuery插件实现大图全屏图片相册
2015/03/14 Javascript
JavaScript与jQuery实现的闪烁输入效果
2016/02/18 Javascript
BootStrap3中模态对话框的使用
2017/01/06 Javascript
js-FCC算法-No repeats please字符串的全排列(详解)
2017/05/02 Javascript
JavaScript中Hoisting详解 (变量提升与函数声明提升)
2017/08/18 Javascript
简单实现jQuery弹窗效果
2017/10/30 jQuery
微信小程序获取手机系统信息的方法【附源码下载】
2017/12/07 Javascript
手写简单的jQuery雪花飘落效果实例
2018/04/22 jQuery
[47:03]完美世界DOTA2联赛PWL S3 Galaxy Racer vs Phoenix 第二场 12.10
2020/12/13 DOTA
python 排列组合之itertools
2013/03/20 Python
Python解析xml中dom元素的方法
2015/03/12 Python
python消费kafka数据批量插入到es的方法
2018/12/27 Python
tensorflow获取预训练模型某层参数并赋值到当前网络指定层方式
2020/01/24 Python
Pycharm及python安装详细教程(图解)
2020/07/31 Python
10款最佳Python开发工具推荐,每一款都是神器
2020/10/15 Python
DogBuddy荷兰:找到你最完美的狗保姆
2019/04/17 全球购物
请写出 BOOL flag 与"零值"比较的 if 语句
2016/02/29 面试题
医学生个人求职信范文
2013/09/24 职场文书
小学生检讨书大全
2014/02/06 职场文书
4s店销售经理岗位职责
2014/07/19 职场文书
2015年小学一年级班主任工作总结
2015/05/21 职场文书
新闻通讯稿范文
2015/07/22 职场文书
春节随笔
2015/08/15 职场文书
村党总支部公开承诺书2016
2016/03/25 职场文书
浅谈 JavaScript 沙箱Sandbox
2021/11/02 Javascript
css弧边选项卡的项目实践
2023/05/07 HTML / CSS