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里的JS打印函数
Oct 09 PHP
PHP简介
Oct 09 PHP
利用php递归实现无限分类 格式化数组的详解
Jun 08 PHP
PHP获取浏览器信息类和客户端地理位置的2个方法
Apr 24 PHP
PHP原生函数一定好吗?
Dec 08 PHP
PHP获取ip对应地区和使用网络类型的方法
Mar 11 PHP
什么是OneThink oneThink后台添加插件步骤
Apr 13 PHP
yii2 数据库读写分离配置示例
Feb 10 PHP
php使用include 和require引入文件的区别
Feb 16 PHP
php+ajax 文件上传代码实例
Mar 18 PHP
Laravel服务容器绑定的几种方法总结
Jun 14 PHP
深入理解PHP+Mysql分布式事务与解决方案
Dec 03 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 应用程序安全防范技术研究
2009/09/25 PHP
PHP读取RSS(Feed)简单实例
2014/06/12 PHP
PHP SESSION的增加、删除、修改、查看操作
2015/03/20 PHP
完美解决phpdoc导出文档中@package的warning及Error的错误
2016/05/17 PHP
微信随机生成红包金额算法php版
2016/07/21 PHP
PHP之十六个魔术方法详细介绍
2016/11/01 PHP
php获取文件名称和扩展名的方法
2017/02/07 PHP
thinkphp查询,3.X 5.0方法(亲试可行)
2017/06/17 PHP
使用PHP访问RabbitMQ消息队列的方法示例
2018/06/06 PHP
thinkphp5修改view到根目录实例方法
2019/07/02 PHP
浅析PHP中的 inet_pton 网络函数
2019/12/16 PHP
jQuery 常见操作实现方式和常用函数方法总结
2011/05/06 Javascript
JavaScript实现多维数组的方法
2013/11/20 Javascript
JS中的构造函数详细解析
2014/03/10 Javascript
jquery验证邮箱格式并显示提交按钮
2015/11/07 Javascript
JS解析url查询参数的简单代码
2017/08/06 Javascript
详解vue2.0 使用动态组件实现 Tab 标签页切换效果(vue-cli)
2017/08/30 Javascript
Swiper 4.x 使用方法(移动端网站的内容触摸滑动)
2018/05/17 Javascript
a标签调用js的方法总结
2019/09/05 Javascript
vue data恢复初始化数据的实现方法
2019/10/31 Javascript
如何使用Javascript中的this关键字
2020/05/28 Javascript
python提取图像的名字*.jpg到txt文本的方法
2018/05/10 Python
python  创建一个保留重复值的列表的补码
2018/10/15 Python
Python命令行参数argv和argparse该如何使用
2021/02/08 Python
Shopee马来西亚:随拍即卖,最佳行动电商拍卖平台
2017/06/05 全球购物
在浏览器端如何得到服务器端响应的XML数据
2012/11/24 面试题
给排水专业应届生求职信
2013/10/12 职场文书
制衣厂各岗位职责
2013/12/02 职场文书
单位人事专员介绍信
2014/01/11 职场文书
《我爱祖国》演讲稿1000字
2014/09/26 职场文书
2014年协会工作总结
2014/11/22 职场文书
工厂清洁工岗位职责
2015/02/14 职场文书
毕业生爱心捐书倡议书
2015/04/27 职场文书
奔腾年代观后感
2015/06/09 职场文书
如何使用分区处理MySQL的亿级数据优化
2021/06/18 MySQL
springboot利用redis、Redisson处理并发问题的操作
2021/06/18 Java/Android