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 24 PHP
PHP echo,print,printf,sprintf函数之间的区别与用法详解
Nov 27 PHP
php中mkdir函数用法实例分析
Nov 15 PHP
php判断文件夹是否存在不存在则创建
Apr 09 PHP
分享php分页的功能模块
Jun 16 PHP
2款PHP无限级分类实例代码
Nov 11 PHP
CentOS下与Apache连接的PHP多版本共存方案实现详解
Dec 19 PHP
Zend Framework教程之Zend_Config_Ini用法分析
Mar 23 PHP
PHP下载远程图片的几种方法总结
Apr 07 PHP
让Laravel API永远返回JSON格式响应的方法示例
Sep 05 PHP
PHP实现数组向任意位置插入,删除,替换数据操作示例
Apr 05 PHP
php查看一个变量的占用内存的实例代码
Mar 29 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生成图片缩略图的方法
2015/04/07 PHP
PHP SPL 被遗落的宝石【SPL应用浅析】
2018/04/20 PHP
PHP实现图片压缩
2020/09/09 PHP
window.open的功能全解析
2006/10/10 Javascript
use jscript with List Proxy Server Information
2007/06/11 Javascript
JS URL传中文参数引发的乱码问题
2009/09/02 Javascript
js本身的局限性 别让javascript做太多事
2010/03/23 Javascript
js列举css中所有图标的实现代码
2011/07/04 Javascript
深入理解JavaScript系列(1) 编写高质量JavaScript代码的基本要点
2012/01/15 Javascript
js原型链原理看图说明
2012/07/07 Javascript
js控制表单不能输入空格的小例子
2013/11/20 Javascript
JS文本获得焦点清除文本文字的示例代码
2014/01/13 Javascript
JavaScript基础函数整理汇总
2015/01/30 Javascript
学习JavaScript设计模式之代理模式
2016/01/12 Javascript
jQuery 特性操作详解及实例代码
2016/09/29 Javascript
javascript 判断是否是微信浏览器的方法
2016/10/09 Javascript
原生js实现新闻列表展开/收起全文功能
2017/01/20 Javascript
Layui组件Table绑定行点击事件和获取行数据的方法
2018/08/19 Javascript
微信小程序template模板与component组件的区别和使用详解
2019/05/22 Javascript
javascript设计模式 ? 策略模式原理与用法实例分析
2020/04/21 Javascript
vue style width a href动态拼接问题的解决
2020/08/07 Javascript
微信小程序获取当前时间及星期几的实例代码
2020/09/20 Javascript
JS闭包原理及其使用场景解析
2020/12/03 Javascript
[14:50]2018DOTA2亚洲邀请赛开幕式
2018/04/03 DOTA
[20:39]DOTA2-DPC中国联赛 正赛开幕式 1月18日
2021/03/11 DOTA
python版本的读写锁操作方法
2016/04/25 Python
python生成随机图形验证码详解
2017/11/08 Python
python并发2之使用asyncio处理并发
2017/12/21 Python
Python操作MongoDB数据库的方法示例
2018/01/04 Python
pycharm中成功运行图片的配置教程
2018/10/28 Python
Python流程控制 if else实现解析
2019/09/02 Python
Python Mock模块原理及使用方法详解
2020/07/07 Python
普通PHP程序员笔试题
2016/01/01 面试题
幼儿园毕业家长感言
2014/02/10 职场文书
大专生求职信
2014/06/29 职场文书
总结Python使用过程中的bug
2021/06/18 Python