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 相关文章推荐
Discuz 模板引擎的封装类代码
Jul 18 PHP
php 小乘法表实现代码
Jul 16 PHP
超级好用的一个php上传图片类(随机名,缩略图,加水印)
Jun 30 PHP
fleaphp常用方法分页之Pager使用方法
Apr 23 PHP
使用PHP遍历文件夹与子目录的函数代码
Sep 26 PHP
php 搜索框提示(自动完成)实例代码
Feb 05 PHP
PHP冒泡算法详解(递归实现)
Nov 10 PHP
PHP进程通信基础之信号量与共享内存通信
Feb 19 PHP
PHP小白必须要知道的php基础知识(超实用)
Oct 10 PHP
PHP观察者模式示例【Laravel框架中有用到】
Jun 15 PHP
laravel 去掉index.php伪静态的操作方法
Oct 12 PHP
TP3.2框架分页相关实现方法分析
Jun 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
全国FM电台频率大全 - 1 北京市
2020/03/11 无线电
PHP用SAX解析XML的实现代码与问题分析
2011/08/22 PHP
php中switch与ifelse的效率区别及适用情况分析
2015/02/12 PHP
php在数组中查找指定值的方法
2015/03/17 PHP
laravel学习笔记之模型事件的几种用法示例
2017/08/15 PHP
JavaScript 学习笔记 Black.Caffeine 09.11.28
2009/11/30 Javascript
Javascript调用C#代码
2011/01/17 Javascript
js实现addClass,removeClass,hasClass的函数代码
2011/07/13 Javascript
js和jQuery设置Opacity半透明 兼容IE6
2016/05/24 Javascript
移动端点击态处理的三种实现方式
2017/01/12 Javascript
vue2.X组件学习心得(新手必看篇)
2017/07/05 Javascript
分析JS中this引发的bug
2017/12/12 Javascript
nodejs实现的连接MySQL数据库功能示例
2018/01/25 NodeJs
vue-router的使用方法及含参数的配置方法
2018/11/13 Javascript
this.$toast() 了解一下?
2019/04/18 Javascript
详解jQuery中的prop()使用方法
2020/01/05 jQuery
Vue实现腾讯云点播视频上传功能的实现代码
2020/08/17 Javascript
python使用mysqldb连接数据库操作方法示例详解
2013/12/03 Python
python3图片转换二进制存入mysql
2013/12/06 Python
在Python的Flask框架中使用模版的入门教程
2015/04/20 Python
python中循环语句while用法实例
2015/05/16 Python
python验证码识别的实例详解
2016/09/09 Python
python matplotlib坐标轴设置的方法
2017/12/05 Python
python 3.6.2 安装配置方法图文教程
2018/09/18 Python
在pycharm中设置显示行数的方法
2019/01/16 Python
从多个tfrecord文件中无限读取文件的例子
2020/02/17 Python
python GUI库图形界面开发之PyQt5切换按钮控件QPushButton详细使用方法与实例
2020/02/28 Python
Shopee马来西亚:随拍即卖,最佳行动电商拍卖平台
2017/06/05 全球购物
新农村建设标语
2014/06/24 职场文书
工地门卫岗位职责范本
2014/07/01 职场文书
大学生实习证明范文(5篇)
2014/09/18 职场文书
2014年纪检部工作总结
2014/11/12 职场文书
2015幼儿园新学期寄语
2015/02/27 职场文书
毕业论文致谢范文
2015/05/14 职场文书
纪委立案决定书
2015/06/24 职场文书
导游词之无锡梅园
2019/11/28 职场文书