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大小写问题:函数名和类名不区分,变量名区分
Jun 17 PHP
如何利用PHP执行.SQL文件
Jul 05 PHP
php之curl设置超时实例
Nov 03 PHP
php生成过去100年下拉列表的方法
Jul 20 PHP
php 如何禁用eval() 函数实例详解
Dec 01 PHP
微信公众平台开发(五) 天气预报功能开发
Dec 03 PHP
详解php几行代码实现CSV格式文件输出
Jul 01 PHP
PHP中如何使用Redis接管文件存储Session详解
Nov 28 PHP
PHP递归的三种常用方式
Feb 28 PHP
Laravel如何创建服务器提供者实例代码
Apr 15 PHP
浅析PHP7 的垃圾回收机制
Sep 06 PHP
php设计模式之观察者模式定义与用法经典示例
Sep 19 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中将网址转换为超链接的函数
2011/09/02 PHP
ajax取消挂起请求的处理方法
2013/03/18 PHP
php数组编码转换示例详解
2014/03/11 PHP
Laravel5.* 打印出执行的sql语句的方法
2017/07/24 PHP
window.location.href IE下跳转失效的解决方法
2014/03/27 Javascript
深入理解ECMAScript的几个关键语句
2016/06/01 Javascript
微信小程序  modal详解及实例代码
2016/11/09 Javascript
用nodejs搭建websocket服务器
2017/01/23 NodeJs
Javascript中的async awai的用法
2017/05/17 Javascript
AngularJS实时获取并显示密码的方法
2018/02/06 Javascript
JQuery通过后台获取数据遍历到前台的方法
2018/08/13 jQuery
vuejs实现折叠面板展开收缩动画效果
2018/09/06 Javascript
ionic使用angularjs表单验证(模板验证)
2018/12/12 Javascript
在VUE中实现文件下载并判断状态的方法
2019/11/08 Javascript
使用Python进行新浪微博的mid和url互相转换实例(10进制和62进制互算)
2014/04/25 Python
python使用三角迭代计算圆周率PI的方法
2015/03/20 Python
Python矩阵常见运算操作实例总结
2017/09/29 Python
python嵌套字典比较值与取值的实现示例
2017/11/03 Python
python并发编程之线程实例解析
2017/12/27 Python
解决pandas使用read_csv()读取文件遇到的问题
2018/06/15 Python
Python操作MySQL数据库的两种方式实例分析【pymysql和pandas】
2019/03/18 Python
Python OrderedDict的使用案例解析
2019/10/25 Python
带你学习Python如何实现回归树模型
2020/07/16 Python
利用CSS3实现的文字定时向上滚动
2016/08/29 HTML / CSS
2019年Java面试必问之经典试题
2012/09/12 面试题
幼儿园保育员辞职信
2014/01/12 职场文书
《邮票齿孔的故事》教学反思
2014/02/22 职场文书
励志演讲稿3分钟
2014/08/21 职场文书
2014五年级班主任工作总结
2014/12/05 职场文书
2015年度保密工作总结
2015/04/24 职场文书
2015年全国爱眼日活动方案
2015/05/05 职场文书
实验室安全管理制度
2015/08/05 职场文书
2016参观监狱警示教育活动心得体会
2016/01/15 职场文书
看看如何用Python绘制小米新版天价logo
2021/04/20 Python
Python实现天气查询软件
2021/06/07 Python
Windows server 2022创建创建林、域树、子域的步骤
2022/06/25 Servers