pdo封装类
<?php
//http://www.imavex.com/php-pdo-wrapper-class/
class db extends PDO
{
private $error;
private $sql;
private $bind; public function __construct($dsn, $user = "", $passwd = "")
{
$options = array(
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8';",
//PDO::ATTR_PERSISTENT => true, //长连接方式切换数据库暂不支持
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_EMULATE_PREPARES => false //禁止php的prepare拼接sql,分两部分传递,彻底杜绝sql注入。
); try {
parent::__construct($dsn, $user, $passwd, $options);
} catch (PDOException $e) {
$this->error = $e->getMessage();
throw new Exception($e);
}
} public function select($table, $fields = "", $where = "", $bind = array())
{
$sql = "SELECT " . $fields . " FROM " . $table;
if (!empty($where))
$sql .= " WHERE " . $where;
$sql .= ";";
return $this->run($sql, $bind);
} public function update($table, Array $info, $where, $bind = array())
{
$fields = $this->filter($table, $info); $sql = "UPDATE " . $table . " SET ";
for ($f = 0; $f < count($fields); ++$f) {
if ($f > 0)
$sql .= ", ";
$sql .= $fields[$f] . ' = ?';
}
$sql .= " WHERE " . $where . ";"; $bind = $this->cleanup($bind);
for ($i = count($fields) - 1; $i >= 0; $i--)
array_unshift($bind, $info[$fields[$i]]); return $this->run($sql, $bind);
} public function insert($table, Array $info)
{
$first = current($info);
if (!is_array($first)) { //如果是插入单个记录,组装成多个的形式
$info = array($info);
}
$fields = $this->filter($table, $info[0]);
$place_holders = '';
foreach ($info as $key => $v) {
if ($key > 0 ) {
$place_holders .= ',';
}
$place_holders .= '(' . implode(',', array_fill(0, count($fields), '?')) . ')';
}
$sql = "INSERT INTO " . $table . " (" . implode($fields, ", ") . ") VALUES $place_holders;";
$bind = array();
foreach ($info as $v) {
foreach ($fields as $field) {
array_push($bind, $v[$field]);
}
}
return $this->run($sql, $bind);
} public function delete($table, $where, $bind = array())
{
$sql = "DELETE FROM " . $table . " WHERE " . $where . ";";
$this->run($sql, $bind);
} private function filter($table, Array $info)
{
$driver = $this->getAttribute(PDO::ATTR_DRIVER_NAME);
if ($driver == 'sqlite') {
$sql = "PRAGMA table_info('" . $table . "');";
$key = "name";
} elseif ($driver == 'mysql') {
$sql = "DESCRIBE " . $table . ";";
$key = "Field";
} else {
$sql = "SELECT column_name FROM information_schema.columns WHERE table_name = '" . $table . "';";
$key = "column_name";
} if (false !== ($list = $this->run($sql))) {
$fields = array();
foreach ($list as $record)
$fields[] = $record[$key];
return array_values(array_intersect($fields, array_keys($info)));
}
return array();
} private function cleanup(Array $bind)
{
if (!is_array($bind)) {
if (!empty($bind))
$bind = array($bind);
else
$bind = array();
}
return $bind;
} public function run($sql, $bind = array())
{
$this->sql = trim($sql);
$this->bind = $this->cleanup($bind);
$this->error = ""; //echo $realSql = $this->getRealSql() . "<BR>"; //获取真实sql,方便调试,如果是绑定模式,参数不一定是真实的(会被过滤),要去mysql日志下面看 try {
$pdostmt = $this->prepare($this->sql);
if ($pdostmt->execute($this->bind) !== false) {
if (preg_match("/^(" . implode("|", array("select", "describe", "pragma")) . ") /i", $this->sql))
return $pdostmt->fetchAll(PDO::FETCH_ASSOC);
elseif (preg_match("/^(" . implode("|", array("delete", "insert", "update")) . ") /i", $this->sql))
return $pdostmt->rowCount();
}
} catch (PDOException $e) {
//echo $this->error = $e->getMessage();
throw new Exception($e); //异常外抛,便于外面捕捉
return false;
}
} private function getRealSql()
{
$realSql = $this->sql;
if (count($realSql) > 0) {
foreach ($this->bind as $v) {
if (!is_numeric($v)) $v = '\'' . $v . '\'';
$realSql = preg_replace('/\?/', $v, $realSql, 1);
}
}
return $realSql;
}
}
pdo封装类的更多相关文章
- 自定义PDO封装类
<?php class Db { protected static $_instance = null; // 定义标识符(通过$_instance值得改变情况,来判定Model类是否被实例化) ...
- php pdo封装类
class MYPDO { protected static $_instance = null; protected $dbname = ''; protected $dsn; protected ...
- 学习到目前,自己封装的db类和pdo类
DB封装类 <?php class DBDA { public $host = "localhost"; public $uid = "root"; pu ...
- 写了个简单的pdo的封装类
<?php class PD { //造对象 public $dsn = "mysql:dbname=test2;host=localhost"; //数据库类型,数据库名和 ...
- php数据库操作封装类
<?php /** * Desc: php操作mysql的封装类 * Author zhifeng * Date: 2015/04/15 * 连接模式:PDO */ class MMysql { ...
- PDO 增删改查封装的类
Selecting Data 你在mysql_*中是这样做的 <?php $result = mysql_query('SELECT * from table') or die(mysql_er ...
- 比Mysqli操作数据库更简便的方式 。PDO
下面来说一下PDO 先画一张图来了解一下 mysqli是针对mysql这个数据库扩展的一个类 PDO是为了能访问更多数据库 如果出现程序需要访问其他数据库的话就可以用PDO来做 PDO数据访问抽象层1 ...
- pdo的使用
PHP 数据对象 (PDO) 扩展为PHP访问数据库定义了一个轻量级的一致接口. PDO 提供了一个数据访问抽象层,这意味着,不管使用哪种数据库,都可以用相同的函数(方法)来查询和获取数据. PDO随 ...
- PHP中PDO事务的使用方法
事务 (Transaction) 是操作数据库中很重要的一个功能, 它可以让你预定一条, 或者一系列 SQL 语句, 然后一起执行. 在执行的过程中, 如果其中的某条执行失败, 可以回滚所有已更改的操 ...
随机推荐
- kmp算法详解
转自:http://blog.csdn.net/ddupd/article/details/19899263 KMP算法详解 KMP算法简介: KMP算法是一种高效的字符串匹配算法,关于字符串匹配最简 ...
- Struts2 Struts.xml DTD 说明
转自:http://blog.csdn.net/cuixiuqin1954/article/details/4228741 关于Struts2中的核心配置文件struts.xml,就其DTD(Docu ...
- 获取MSSQL Server中的相关信息(视图、存储过程、触发器、表)
在SQL SERVER得到某个数据库下面所有的表.视图.存储过程.触发器 select name from sysobjects where xtype='TR' --所有触发器select name ...
- 2016.6.20 计算机网络复习要点第三章之CSMA/CD协议
1.最早的以太网是将许多计算机都连接到一根总线上: (1)总线的特点是:当一台计算机发送数据时,总线上的所有计算机都检测到这个数据,这种就是广播通信方式: (2)为了实现在总线上的一对一通信,可以使每 ...
- maven 编译报错 “找不到符号”
报错如下: E:\workspace\iccardcore\mis\src\main\java\com\hxsmart\sicard\core\webapp\action\process\DayEnd ...
- 不容易系列之二[HDU2042]
不容易系列之二 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- 导出一个EXCEL,多个SHEET
Infragistics.Excel. Workbook work = new Infragistics.Excel.Workbook(); Infra ...
- web farm 讨论引出
关于web farm 有成功的实施的文档没 用它还不如 用nginx,简单易用. Nginx for windows的运行效果咋样 windows iis无敌 玩nginx就不要用win系统,必须l ...
- 【BZOJ】1503: [NOI2004]郁闷的出纳员(Splay)
http://www.lydsy.com/JudgeOnline/problem.php?id=1503 这题没有看题解就1a了-好开心,, 其实后面去看题解发现他们的都很麻烦,其实有种很简单的做法: ...
- Java正则表达式教程
地址:http://www.java3z.com/cwbwebhome/article/article8/Regex/Java.Regex.Tutorial.html#reg0_1