<?php
header("Content-Type:text/html;charset=utf-8"); class PdoMysql{
public static $config = array();//设置连接参数,配置信息
public static $link = null;//保存连接标识符
public static $pconnect = false;//是否开启长连接
public static $dbVersion = null;//保存数据库版本
public static $connected = false;//判断是否连接成功
public static $PDOStatement = null;//保证PDOStatement对象
public static $queryStr = null;//保存最后执行的操作
public static $error = null;//保存错误信息
public static $lastInsertId = null;//保存上一步插入操作保存的AUTO_INCREMANT
public static $numRows = null;//受影响记录的条数 /**
* 构造函数,连接数据库
*
* @param array|string $dbConfig The database configuration
*
* @return boolean ( description_of_the_return_value )
*/
public function __construct($dbConfig=''){
if(!class_exists("PDO")){
self::throw_exception("不支持PDO,请先开启");
}
if(!is_array($dbConfig)){
$dbConfig = array(
'hostname' => 'localhost',
'username' => 'root',
'password' => '1234',
'database' => 'test',
'hostport' => '3306',
'dbms' => 'mysql',
'dsn' => 'mysql:host=localhost;dbname=test'
);
}
if(empty($dbConfig['hostname'])){
self::throw_exception("没有定义数据库配置,请先定义");
} self::$config = $dbConfig; if(empty(self::$config['params'])){
self::$config['params'] = array();
} if(!isset(self::$link)){
$configs = self::$config;
if(self::$pconnect){
//开启长连接,添加到配置数组中
$configs['params'][constant("PDO::ATTR_PERSISTENT")] = true;
} try {
self::$link = new PDO($configs['dsn'],$configs['username'],$configs['password'],$configs['params']);
} catch (PDOException $e) {
self::throw_exception($e->getMessage());
} if(!self::$link){
self::throw_exception("PDO连接错误");
return false;
}
self::$link->exec("set names utf8");
self::$dbVersion = self::$link->getAttribute(constant("PDO::ATTR_SERVER_VERSION"));
unset($configs);
}
} /**
* 得到所有记录
*
* @param <type> $sql The sql
*
* @return <type> All.
*/
public static function getAll($sql=null){
if($sql!=null){
self::query($sql);
}
$result = self::$PDOStatement->fetchAll(constant("PDO::FETCH_ASSOC"));
return $result;
} /**
* 得到一条记录
*
* @param <type> $sql The sql
*
* @return <type> The row.
*/
public static function getRow($sql=null){
if($sql!=null){
self::query($sql);
}
$result = self::$PDOStatement->fetch(constant("PDO::FETCH_ASSOC"));
return $result;
} /**
* 执行增删改操作,返回受影响记录的条数
*
* @param <type> $sql The sql
*
* @return boolean ( description_of_the_return_value )
*/
public static function execute($sql=null){
$link = self::$link;
if(!$link)return false;
if($sql!=null){
self::$queryStr = $sql;
}
if(!empty(self::$PDOStatement))self::free();
$result = $link->exec(self::$queryStr);
self::haveErrorThrowException();
if($result){
self::$lastInsertId = $link->lastInsertId();
self::$numRows = $result;
return $result;
}else{
return false;
}
} /**
* 根据主键查找记录
*
* @param <type> $tabName The tab name
* @param <type> $priId The pri identifier
* @param string $fields The fields
*
* @return <type> ( description_of_the_return_value )
*/
public static function findById($tabName,$priId,$fields='*'){
$sql = 'SELECT %s FROM %s WHERE id=%d';
return self::getRow(sprintf($sql,self::parseFields($fields),$tabName,$priId));
} /**
* 执行普通查询
*
* @param <type> $tables The tables
* @param <type> $where The where
* @param string $fields The fields
* @param <type> $group The group
* @param <type> $having The having
* @param <type> $order The order
* @param <type> $limit The limit
*
* @return <type> ( description_of_the_return_value )
*/
public static function find($tables,$where=null,$fields='*',$group=null,$having=null,$order=null,$limit
=null){
$sql = 'SELECT '.self::parseFields($fields).' FROM '.$tables
.self::parseWhere($where)
.self::parseGroup($group)
.self::parseHaving($having)
.self::parseOrder($order)
.self::parseLimit($limit);
$data = self::getAll($sql);
return $data;
} /**
* 添加记录
*
* @param <type> $data The data
* @param <type> $table The table
*
* @return <type> ( description_of_the_return_value )
*/
public static function add($data,$table){
$keys = array_keys($data);
array_walk($keys, array('PdoMySQL','addSpecialChar'));
$fieldsStr = join(',',$keys);
$values = "'".join("','",array_values($data))."'";
$sql = "INSERT {$table}({$fieldsStr}) VALUES({$values})";
return self::execute($sql);
} /**
* 更新数据
*
* @param <type> $data The data
* @param <type> $table The table
* @param <type> $where The where
* @param <type> $order The order
* @param <type> $limit The limit
*/
public static function update($data,$table,$where=null,$order=null,$limit=null){
$sets = '';
foreach ($data as $key => $value) {
$sets .= $key."='".$value."',";
}
$sets = rtrim($sets,',');
$sql = "UPDATE {$table} SET {$sets}".self::parseWhere($where).self::parseOrder($order).self::parseLimit($limit);
echo $sql;
} /**
* 删除数据
*
* @param <type> $data The data
* @param <type> $table The table
* @param <type> $where The where
* @param <type> $order The order
* @param <type> $limit The limit
*
* @return <type> ( description_of_the_return_value )
*/
public static function delete($table,$where=null,$order=null,$limit=null){
$sql = "DELETE FROM {$table} ".self::parseWhere($where).self::parseOrder($order).self::parseLimit($limit);
return self::execute($sql);
} /**
* 执行查询
*
* @param string $sql The sql
*
* @return boolean ( description_of_the_return_value )
*/
public static function query($sql=''){
$link = self::$link;
if(!$link)return false; //判断之前是否有结果集,如果有的话,释放结果集
if(!empty(self::$PDOStatement))self::free();
self::$queryStr = $sql;
self::$PDOStatement = $link->prepare(self::$queryStr);
$res = self::$PDOStatement->execute();
self::haveErrorThrowException();
return $res;
} /**
* 获取最后执行的sql
*
* @return boolean The last sql.
*/
public static function getLastSql(){
$link = self::$link;
if(!$link){
return false;
}
return self::$queryStr;
} /**
* 获取最后插入的ID
*
* @return boolean The last insert identifier.
*/
public static function getLastInsertId(){
$link = self::$link;
if(!$link){
return false;
}
return self::$lastInsertId;
} /**
* 获得数据库的版本
*
* @return boolean The database version.
*/
public static function getDbVersion(){
$link = self::$link;
if(!$link){
return false;
}
return self::$dbVersion;
} /**
* 得到数据库中表
*
* @return array ( description_of_the_return_value )
*/
public static function showTables(){
$tables = array();
if(self::query("show tables")){
$result = self::getAll();
foreach ($result as $key => $value) {
$tables[$key] = current($value);
}
}
return $tables;
} /**
* 解析where条件
*
* @param <type> $where The where
*
* @return <type> ( description_of_the_return_value )
*/
public static function parseWhere($where){
$whereStr = '';
if(is_string($where)&&!empty($where)){
$whereStr = $where;
}
return empty($whereStr) ? '' : ' WHERE '.$whereStr;
} /**
* 解析group
*
* @param <type> $group The group
*
* @return <type> ( description_of_the_return_value )
*/
public static function parseGroup($group){
$groupStr = '';
if(is_array($group)){
$groupStr = implode(',', $group);
}elseif(is_string($group)&&!empty($group)){
$groupStr = $group;
}
return empty($groupStr) ? '' : ' GROUP BY '.$groupStr;
} /**
* 解析having
*
* @param <type> $having The having
*
* @return <type> ( description_of_the_return_value )
*/
public static function parseHaving($having){
$havingStr = '';
if(is_string($having)&&!empty($having)){
$havingStr = $having;
}
return empty($havingStr) ? '' : ' HAVING '.$havingStr;
} /**
* 解析order
*
* @param <type> $order The order
*
* @return <type> ( description_of_the_return_value )
*/
public static function parseOrder($order){
$orderStr = '';
if(is_string($order)&&!empty($order)){
$orderStr = $order;
}
return empty($orderStr) ? '' : ' ORDER BY '.$orderStr;
} /**
* 解析limit
*
* @param <type> $limit The limit
*
* @return <type> ( description_of_the_return_value )
*/
public static function parseLimit($limit){
$limitStr = '';
if(is_array($limit)){
$limitStr = implode(',', $limit);
}elseif(is_string($limit)&&!empty($limit)){
$limitStr = $limit;
}
return empty($limitStr) ? '' : ' LIMIT '.$limitStr;
} /**
* 解析字段
*
* @param <type> $fields The fields
*
* @return string ( description_of_the_return_value )
*/
public static function parseFields($fields){
if(is_array($fields)){
array_walk($fields, array('PdoMySQL','addSpecialChar'));
$fieldsStr = implode(',', $fields);
}elseif (is_string($fields)&&!(empty($fields))) {
if(strpos($fields, '`')===false){
$fields = explode(',', $fields);
array_walk($fields, array('PdoMySQL','addSpecialChar'));
$fieldsStr = implode(',', $fields);
}else{
$fieldsStr = $fields;
}
}else{
$fieldsStr = "*";
}
return $fieldsStr;
} /**
* 通过反引号引用字字段
*
* @param string $value The value
*
* @return string ( description_of_the_return_value )
*/
public static function addSpecialChar(&$value){
if($value==="*"||strpos($value,'.')!==false||strpos($value,'`')!==false){
//不用做处理
}elseif(strpos($value, '`')===false){
$value = '`'.trim($value).'`';
}
return $value;
} /**
* 释放结果集
*/
public static function free(){
self::$PDOStatement = null;
} /**
* 抛出错误信息
*
* @return boolean ( description_of_the_return_value )
*/
public static function haveErrorThrowException(){
$obj = empty(self::$PDOStatement) ? self::$link : self::$PDOStatement;
$arrError = $obj->errorInfo();
if($arrError[0]!='00000'){
self::$error = 'SQLSTATE=>'.$arrError[0].'<br/>SQL Error=>'.$arrError[2].'<br/>Error SQL=>'.self::$queryStr;
self::throw_exception(self::$error);
return false;
}
if(self::$queryStr==''){
self::throw_exception('没有执行SQL语句');
return false;
}
} /**
* 自定义错误处理
*
* @param <type> $errMsg The error message
*/
public static function throw_exception($errMsg){
echo $errMsg;
} /**
* 销毁连接对象,关闭数据库
*/
public static function close(){
self::$link = null;
}
}
$pdo = new PdoMysql();
var_dump($pdo->showTables());

  

php封装pdo操作数据的工具类的更多相关文章

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

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

  2. JBPM4入门——4.封装流程管理的工具类(JbpmUtil)

    本博文只是简要对JBPM4进行介绍,如需更详细内容请自行google 链接: JBPM入门系列文章: JBPM4入门——1.jbpm简要介绍 JBPM4入门——2.在eclipse中安装绘制jbpm流 ...

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

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

  4. jdbc 11: 封装自己的jdbc工具类

    jdbc连接mysql,封装自己的jdbc工具类 package com.examples.jdbc.utils; import java.sql.*; import java.util.Resour ...

  5. java模板模式项目中使用--封装一个http请求工具类

    需要调用http接口的代码继承FundHttpTemplate类,重写getParamData方法,在getParamDate里写调用逻辑. 模板: package com.crb.ocms.fund ...

  6. Java 基于mail.jar 和 activation.jar 封装的邮件发送工具类

    准备工作 发送邮件需要获得协议和支持! 开启服务 POP3/SMTP 服务 如何开启 POP3/SMTP 服务:https://www.cnblogs.com/pojo/p/14276637.html ...

  7. Redis进阶实践之九 独立封装的RedisClient客户端工具类

    一.引言 今天开始有关Redis学习的第九篇文章了,以后肯定会大量系统使用Redis作为缓存介质,为了更好的更好的Redis,自己写了两个工具类,但是这两个工具类,没有提供一致的接口,是为了使用的独立 ...

  8. 基于AFNetworking封装的网络请求工具类【原创】

    今天给大家共享一个我自己封装的网络请求类,希望能帮助到大家. 前提,导入AFNetworking框架, 关于修改AFN源码:通常序列化时做对text/plan等的支持时,可以一劳永逸的修改源代码,在a ...

  9. java生成图片验证码(转)--封装生成图片验证码的工具类

    博客部分内容转载自 LonlySnow的博客:后台java 实现验证码生成 1.controller方法 @RequestMapping(value = "/verifycode/img&q ...

随机推荐

  1. EasyPlayer RTSP播放器对RTSP播放地址url的通用兼容修改意见

    问题反馈 最近在线上遇到一位老朋友咨询关于EasyPlayer播放器的事情,大概现象就是分别用EasyPlayer和vlc播放大华摄像机的RTSP流,流地址是:rtsp://admin:admin12 ...

  2. js实现网页中的"运行代码"功能

    <!DOCTYPE html> <html> <head> <meta charset='utf8' /> <title>网页中的运行代码功 ...

  3. Hadoop实战-Flume之Sink Failover(十六)

    a1.sources = r1 a1.sinks = k1 k2 a1.channels = c1 # Describe/configure the source a1.sources.r1.type ...

  4. Spring/Java error: namespace element 'annotation-config' … on JDK 1.5 and higher

    Extract the jar file: mkdir spring cd spring jar xvf ../spring.jar Check the Spring version in META- ...

  5. 微信小程序开发:学习笔记[3]——WXSS样式

    微信小程序开发:学习笔记[3]——WXSS样式 快速开始 介绍 WXSS(WeiXin Style Sheets)是一套用于小程序的样式语言,用于描述WXML的组件样式,也就是视觉上的效果. WXSS ...

  6. POJ - 1426 Find The Multiple 【DFS】

    题目链接 http://poj.org/problem?id=1426 题意 给出一个数 要求找出 只有 0 和 1 组成的 十进制数字 能够整除 n n 不超过 200 十进制数字位数 不超过100 ...

  7. 小米系列手机调试Installation failed with message Failed to establish session

    用Android studio 2.3调度程序时提示"Installation failed with message Failed to establish session"错误 ...

  8. myeclipse内存不足的处理

    Myeclipse内存溢出解决方案 1.tomcat内存扩展 修改tomcat中bin目录下catalina.bat文件在echo Using CATALINA_BASE:  "%CATAL ...

  9. iOS Assertion failure in -[UITableView _classicHeightForRowAtIndexPath:]

    Assertion failure in -[UITableView _classicHeightForRowAtIndexPath:], /SourceCache/UIKit_Sim/UIKit-3 ...

  10. mysql八:ORM框架SQLAlchemy

    阅读目录 一 介绍 二 创建表 三 增删改查 四 其他查询相关 五 正查.反查 一 介绍 SQLAlchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API之上,使用关系对象映射进 ...