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实现jQuery扩展函数
Oct 30 PHP
php+javascript的日历控件
Nov 19 PHP
比较好用的PHP防注入漏洞过滤函数代码
Apr 11 PHP
PHP CodeBase:将时间显示为&quot;刚刚&quot;&quot;n分钟/小时前&quot;的方法详解
Jun 06 PHP
discuz加密解密函数使用方法和中文注释
Jan 21 PHP
php switch语句多个值匹配同一代码块应用示例
Jul 29 PHP
深入分析PHP引用(&amp;)
Sep 04 PHP
PHP sleep()函数, usleep()函数
Aug 25 PHP
php通过执行CutyCapt命令实现网页截图的方法
Sep 30 PHP
php使用curl代理实现抓取数据的方法
Feb 03 PHP
php+redis消息队列实现抢购功能
Feb 08 PHP
php封装的page分页类完整实例代码
Feb 01 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
要会喝咖啡也要会知道咖啡豆
2021/03/03 咖啡文化
深入理解用mysql_fetch_row()以数组的形式返回查询结果
2013/06/05 PHP
分享PHP header函数使用教程
2013/09/05 PHP
Joomla数据库操作之JFactory::getDBO用法
2016/05/05 PHP
smarty的section嵌套循环用法示例
2016/05/28 PHP
PHP实现求两个字符串最长公共子串的方法示例
2017/11/17 PHP
详解阿里云视频直播PHP-SDK接入教程
2020/07/09 PHP
javascript 仿QQ滑动菜单效果代码
2010/09/03 Javascript
禁用页面部分JavaScript不是全部而是部分
2014/09/03 Javascript
nodejs下打包模块archiver详解
2014/12/03 NodeJs
JS实现控制表格只显示行边框或者只显示列边框的方法
2015/03/31 Javascript
javascript引用类型指针的工作方式
2015/04/13 Javascript
JavaScript编程中的Promise使用大全
2015/07/28 Javascript
JS添加或修改控件的样式(Class)实现方法
2016/10/15 Javascript
JavaScript三种绑定事件方式及相互之间的区别分析
2017/01/10 Javascript
浅谈JavaScript异步编程
2017/01/20 Javascript
JS中Object对象的原型概念基础
2018/01/29 Javascript
nodejs搭建本地服务器轻松解决跨域问题
2018/03/21 NodeJs
小程序实现上下移动切换位置
2019/09/23 Javascript
Vue 请求传公共参数的操作
2020/07/31 Javascript
JS如何操作DOM基于表格动态展示数据
2020/10/15 Javascript
node.js通过url读取文件
2020/10/16 Javascript
matplotlib subplots 设置总图的标题方法
2018/05/25 Python
pandas中的series数据类型详解
2019/07/06 Python
Python 占位符的使用方法详解
2019/07/10 Python
python 判断三个数字中的最大值实例代码
2019/07/24 Python
Lookfantastic阿联酋官网:英国知名美妆护肤购物网站
2020/05/26 全球购物
美术师范毕业生自荐信
2013/11/16 职场文书
师范教师大学生职业生涯规划范文
2014/01/05 职场文书
企业军训感言
2014/02/08 职场文书
《争吵》教学反思
2014/02/15 职场文书
日化店促销方案
2014/03/26 职场文书
学校运动会广播稿范文
2014/10/02 职场文书
社区党风廉政建设调研报告
2015/01/01 职场文书
python tkinter实现定时关机
2021/04/21 Python
Python基于百度API识别并提取图片中文字
2021/06/27 Python