php5.5后就不支持mysql扩展了,也就是说这以后都不能使用msyql_conncet之类的函数了。不过没有关系,pdo比mysql有更多优势,写法也很简单,下面贴出一个来自互联网的pdo处理类。本来自己写了一个开头,一直没时间完善,现在急用,就在网上找这个现成的,看好不好用。

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();
}
}

一个强大的封装好的pdo处理类的更多相关文章

  1. 毕加索的艺术——Picasso,一个强大的Android图片下载缓存库,OkHttpUtils的使用,二次封装PicassoUtils实现微信精选

    毕加索的艺术--Picasso,一个强大的Android图片下载缓存库,OkHttpUtils的使用,二次封装PicassoUtils实现微信精选 官网: http://square.github.i ...

  2. 一个强大的jquery分页插件

    点击这里查看效果 这个分页插件使用方便,引用keleyidivpager.js和keleyidivpager.css文件,然后在htm(或者php,aspx,jsp等)页面中对分页总数,参数名,前缀后 ...

  3. Dubbo 泛化调用的参数解析问题及一个强大的参数解析工具 PojoUtils

    排查了3个多小时,因为一个简单的错误,发现一个强大的参数解析工具,记录一下. 背景 Nodejs 通过 tether 调用 Java Dubbo 服务.请求类的某个参数对象 EsCondition 有 ...

  4. 一个强大的UI node 抽象

    基于cocos2d -x的一个强大的 界面对象的基类 ---@type uinode ui 对象的抽象 --@usage -- 界面打开的执行流程 -- 带*的是可选重写的函数,不带*的为必须实现的 ...

  5. SpringMVC整合Shiro,Shiro是一个强大易用的Java安全框架,提供了认证、授权、加密和会话管理等功能

    SpringMVC整合Shiro,Shiro是一个强大易用的Java安全框架,提供了认证.授权.加密和会话管理等功能. 第一步:配置web.xml <!-- 配置Shiro过滤器,先让Shiro ...

  6. Advanced Archive Password Recovery (ARCHPR) 是一个强大的压缩包密码破解工具,适用于ZIP和RAR档案的高度优化的口令恢复工具。

    RAR压缩文件密码破解工具是一款简单易用的RAR文档和ZIP文档密码破解软件,如果你不小心忘了解压密码或是下载的RAR文件需要密码,那么均可以使用本软件进行暴力破解.不管WinRAR /RAR 的密码 ...

  7. iOS开发拓展篇—封装音频文件播放工具类

    iOS开发拓展篇—封装音频文件播放工具类 一.简单说明 1.关于音乐播放的简单说明 (1)音乐播放用到一个叫做AVAudioPlayer的类 (2)AVAudioPlayer常用方法 加载音乐文件 - ...

  8. PclZip:强大的PHP压缩与解压缩zip类

    PclZip简介PclZip是一个很强大的压缩与解压缩zip文件的PHP类,PclZip library能够压缩与解压缩Zip格式的压缩档(WinZip.PKZIP):且能对此类类档案进行处理,包括产 ...

  9. Redis进阶实践之九 独立封装的RedisClient客户端工具类(转载9)

    Redis进阶实践之九 独立封装的RedisClient客户端工具类 一.引言 今天开始有关Redis学习的第九篇文章了,以后肯定会大量系统使用Redis作为缓存介质,为了更好的更好的Redis,自己 ...

随机推荐

  1. MongoDB中的映射,限制记录和记录拼排序 文档的插入查询更新删除操作

    映射 在 MongoDB 中,映射(Projection)指的是只选择文档中的必要数据,而非全部数据.如果文档有 5 个字段,而你只需要显示 3 个,则只需选择 3 个字段即可. find() 方法 ...

  2. [LeetCode] 231 Power of Two && 326 Power of Three && 342 Power of Four

    这三道题目都是一个意思,就是判断一个数是否为2/3/4的幂,这几道题里面有通用的方法,也有各自的方法,我会分别讨论讨论. 原题地址:231 Power of Two:https://leetcode. ...

  3. Spring+SpringMVC+MyBatis整合进阶篇(四)RESTful实战(前端代码修改)

    前言 前文<RESTful API实战笔记(接口设计及Java后端实现)>中介绍了RESTful中后端开发的实现,主要是接口地址修改和返回数据的格式及规范的修改,本文则简单介绍一下,RES ...

  4. oracle 权限

    一.介绍这一部分我们主要看看oracle中如何管理权限和角色,权限和角色的区别在哪里.当刚刚建立用户时,用户没有任何权限,也不能执行任何操作.如果要执行某种特定的数据库操作,则必须为其授予系统的权限: ...

  5. iOS蓝牙心得

    1.获取蓝牙mac地址 因为安卓不能得到uuid,所以,在要同步的时候要将uuid转换成mac地址,下面是转换方法 [peripheral discoverServices:@[[CBUUID UUI ...

  6. 01.python基础知识_01

    一.编译型语言和解释型语言的区别是什么? 1.编译型语言将源程序全部编译成机器码,并把结果保存为二进制文件.运行时,直接使用编译好的文件即可 2.解释型语言只在执行程序时,才一条一条的解释成机器语言给 ...

  7. 巧用Mono.Cecil反射加载类型和方法信息

    最近在做服务的细粒度治理,统一管理所有服务的方法.参数.返回值信息.方便后续的各个模块之间的对接和协作. 目前系统中所有的服务,管理到接口契约粒度,即服务接口声明和服务接口实现.要做服务的细粒度治理: ...

  8. Divisors poj2992

    Divisors Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9940   Accepted: 2924 Descript ...

  9. Reverse Words in a String III

    Given a string, you need to reverse the order of characters in each word within a sentence while sti ...

  10. spring 内部工作机制(一)

    Spring内部机制的内容较多,所以打算多分几个阶段来写. 本章仅探索Spring容器启动做了哪些事: 前言: 都说Spring容器就像一台构造精妙的机器,此话一点不假,我们通过配置文件向机器传达控制 ...