<?php
class db extends PDO {
private $error;
private $sql;
private $bind;
private $errorCallbackFunction;
private $errorMsgFormat; public function __construct($dsn, $user="", $passwd="") {
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
); try {
parent::__construct($dsn, $user, $passwd, $options);
} catch (PDOException $e) {
$this->error = $e->getMessage();
}
} private function debug() {
if(!empty($this->errorCallbackFunction)) {
$error = array("Error" => $this->error);
if(!empty($this->sql))
$error["SQL Statement"] = $this->sql;
if(!empty($this->bind))
$error["Bind Parameters"] = trim(print_r($this->bind, true)); $backtrace = debug_backtrace();
if(!empty($backtrace)) {
foreach($backtrace as $info) {
if($info["file"] != __FILE__)
$error["Backtrace"] = $info["file"] . " at line " . $info["line"];
}
} $msg = "";
if($this->errorMsgFormat == "html") {
if(!empty($error["Bind Parameters"]))
$error["Bind Parameters"] = "<pre>" . $error["Bind Parameters"] . "</pre>";
$css = trim(file_get_contents(dirname(__FILE__) . "/error.css"));
$msg .= '<style type="text/css">' . "\n" . $css . "\n</style>";
$msg .= "\n" . '<div class="db-error">' . "\n\t<h3>SQL Error</h3>";
foreach($error as $key => $val)
$msg .= "\n\t<label>" . $key . ":</label>" . $val;
$msg .= "\n\t</div>\n</div>";
}
elseif($this->errorMsgFormat == "text") {
$msg .= "SQL Error\n" . str_repeat("-", 50);
foreach($error as $key => $val)
$msg .= "\n\n$key:\n$val";
} $func = $this->errorCallbackFunction;
$func($msg);
}
} public function delete($table, $where, $bind="") {
$sql = "DELETE FROM " . $table . " WHERE " . $where . ";";
$this->run($sql, $bind);
} private function filter($table, $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($bind) {
if(!is_array($bind)) {
if(!empty($bind))
$bind = array($bind);
else
$bind = array();
}
return $bind;
} public function insert($table, $info) {
$fields = $this->filter($table, $info);
$sql = "INSERT INTO " . $table . " (" . implode($fields, ", ") . ") VALUES (:" . implode($fields, ", :") . ");";
$bind = array();
foreach($fields as $field)
$bind[":$field"] = $info[$field];
return $this->run($sql, $bind);
} public function run($sql, $bind="") {
$this->sql = trim($sql);
$this->bind = $this->cleanup($bind);
$this->error = ""; 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) {
$this->error = $e->getMessage();
$this->debug();
return false;
}
} public function select($table, $where="", $bind="", $fields="*") {
$sql = "SELECT " . $fields . " FROM " . $table;
if(!empty($where))
$sql .= " WHERE " . $where;
$sql .= ";";
return $this->run($sql, $bind);
} public function setErrorCallbackFunction($errorCallbackFunction, $errorMsgFormat="html") {
//Variable functions for won't work with language constructs such as echo and print, so these are replaced with print_r.
if(in_array(strtolower($errorCallbackFunction), array("echo", "print")))
$errorCallbackFunction = "print_r"; if(function_exists($errorCallbackFunction)) {
$this->errorCallbackFunction = $errorCallbackFunction;
if(!in_array(strtolower($errorMsgFormat), array("html", "text")))
$errorMsgFormat = "html";
$this->errorMsgFormat = $errorMsgFormat;
}
} public function update($table, $info, $where, $bind="") {
$fields = $this->filter($table, $info);
$fieldSize = sizeof($fields); $sql = "UPDATE " . $table . " SET ";
for($f = 0; $f < $fieldSize; ++$f) {
if($f > 0)
$sql .= ", ";
$sql .= $fields[$f] . " = :update_" . $fields[$f];
}
$sql .= " WHERE " . $where . ";"; $bind = $this->cleanup($bind);
foreach($fields as $field)
$bind[":update_$field"] = $info[$field]; return $this->run($sql, $bind);
}
}
?>

PDO扩展的更多相关文章

  1. [PDO绑定参数]使用PHP的PDO扩展进行批量更新操作

    最近有一个批量更新数据库表中某几个字段的需求,在做这个需求的时候,使用了PDO做参数绑定,其中遇到了一个坑. 方案选择 笔者已知的做批量更新有以下几种方案: 1.逐条更新 这种是最简单的方案,但无疑也 ...

  2. PDO扩展使用方法

    pdo扩展为php访问数据库提供了一个轻量级的一致接口,pdo提供了一个数据访问抽象层,这意味着不管使用哪种数据库,都可以使用相同的函数来查询和获取数据. $dbms='mysql'; //数据库类型 ...

  3. win7 PHP7.0的PDO扩展

    一个非常棘手的问题,win7(64位)环境,编译安装的mysql,php无法使用pdo扩展. 而我的centos中yum安装的php,pdo是好用的. 百度了一大堆,都无法解决. 基本上百度到的都是要 ...

  4. linux(centos)下安装PHP的PDO扩展

    PHP 数据对象PDO扩展为PHP访问数据库定义了一个轻量级的一致接口.PDO 提供了一个数据访问抽象层,这意味着,不管使用哪种数据库,都可以用相同的函数(方法)来查询和获取数据.最近在我们的建站和O ...

  5. php开启pdo扩展

    在Windows环境下php 5.1以上版本中,pdo和主要数据库的驱动同php一起作为扩展发布,要激活它们只需要简单地编辑php.ini文件. 打开php.ini配置文件,找到extension=p ...

  6. [日常] ubuntu下安装php pdo扩展和导入数据库

    默认安装的php不存在pdo扩展,因此在使用到的时候会报错,直接使用这个命令 apt-get install php-mysql 就可以成功安装pdo扩展 安装完数据库后需要导入sql语句,先进入数据 ...

  7. CentOS8 安装部署Apache+Php+MariaDB(pdo扩展)

    使用新的CentOS8系统架设PHP服务器,因现在主流数据库mysql已闭源了,所以现在改为使用MariaDB.而php7以后不支持mysqli链接,只有pdo方式,为了安装pdo扩展,所以重新编译安 ...

  8. ubuntu下安装pdo扩展

    ubuntu下安装好LAMP后默认情况没有安装mysql_pdo扩展,以下是安装 步聚,在终端输入以下命令 1.pecl search pdo 2.sudo pecl install pdo 当出现E ...

  9. php编译安装增加pdo扩展

    首先查看mysql版本和位置 mysql --version whereis mysql 去php安装目录安装扩展 cd /usr/local/src/php-5.4.25/ext/pdo_mysql ...

  10. PHP PDO扩展整理,包括环境配置\基本增删改查\事务\预处理

    相关文章:PHP的mysql扩展整理,操作数据库的实现过程分析    PHPmysqli扩展整理,包括面向过程和面向对象的比较\事务控制\批量执行\预处理 介绍 PDO是一种PHP程序连接数据库的接口 ...

随机推荐

  1. uva live 7638 Number of Connected Components (并查集)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  2. Collector 源码分析

    Collectors Collectors 配合 stream 可以实现 MapReduce 操作,也可以单独完成流中元素的收集. 收集器接口和实现 /** * 收集器接口 */ public int ...

  3. CSS- 层叠和继承

    层叠和继承 - 学习 Web 开发 | MDN 优先级 - CSS:层叠样式表 | MDN 继承 - CSS:层叠样式表 | MDN CSS 层叠 - CSS:层叠样式表 | MDN

  4. 用Vue来实现音乐播放器(十四):歌手数据接口抓取

    第一步:在api文件夹下创建一个singer.js文件 返回一个getSingerList()方法  使他能够在singer.vue中调用 import jsonp from '../common/j ...

  5. Powershell 邮件发送

    目录 目录 前言 Send-MailMessage NETMail 使用OutLook发送邮件 前言 最近领导想在winServer2012上搞个自动发送邮件的计划任务,下面有几种发送邮件的方式. 如 ...

  6. Numpy的补充(重要!!)

    轴的概念 英文解释  https://www.sharpsightlabs.com/blog/numpy-axes-explained/ 汉化解释 https://www.jianshu.com/p/ ...

  7. 请正视抑郁症(附Zung氏抑郁自评量表(SDS))

    为什么要记录? 因为去年开始关注抑郁症这块,逐渐发现抑郁症原来不只是简单的情绪问题,它是情绪与实质性的生理相互作用并紧密关联的疾病,并不是单纯的劝解自己就可以疗愈的一种疾病,它的存在需要换着身边的人帮 ...

  8. 安全运维 - Windows系统应急响应

    挖矿病毒应急 传播方式: 通过社工.钓鱼方式下载和运行了挖矿程序(邮件.IM等) 利用计算机系统远程代码执行漏洞下载.上传和执行挖矿程序. 利用i算计Web或第三方软件漏洞获取计算机权限,然后下载和执 ...

  9. hacker101教学笔记--introduction--the web in depth

    hacker101笔记 提前准备:运行java的环境 burp proxy(代理) firefox(浏览器) xss 可以控制参数,发送JavaScript到服务器,再从服务器反映到浏览器上面< ...

  10. MySQL-第十篇多表连接查询

    1.SQL92规范.SQL99规范 2.广义笛卡尔积,多表之间没有任何连接条件,得到的结果将是N x M条记录. 3.SQL92中的左外连接.右外连接,连接符有(+或*),放在连接条件那一边就叫做左或 ...