PHP PDO database operation class
A simple PDO class package. . For learning and communication only
PdoDb database class
<?php /** * @throws Error * PDO数据库 */ class PdoDb extends DatabaseAbstract { /** * PDO实例 * @var PDO */ protected $DB; /** * PDO准备语句 * @var PDOStatement */ protected $Stmt; /** * 最后的SQL语句 * @var string */ protected $Sql; /** * 配置信息 $config=array('dsn'=>xxx,'name'=>xxx,'password'=>xxx,'option'=>xxx) * @var array */ protected $Config; /** * 构造函数 * @param array $config */ public function __construct($config) { $this->Config = $config; } /** * 连接数据库 * @return void */ public function connect() { $this->DB = new PDO($this->Config['dsn'], $this->Config['name'], $this->Config['password'], $this->Config['option']); //默认把结果序列化成stdClass $this->DB->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); //自己写代码捕获Exception $this->DB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT); } /** * 断开连接 * @return void */ public function disConnect() { $this->DB = null; $this->Stmt = null; } /** * 执行sql,返回新加入的id * @param string $statement * @return string */ public function exec($statement) { if ($this->DB->exec($statement)) { $this->Sql = $statement; return $this->lastId(); } $this->errorMessage(); } /** * 查询sql * @param string $statement * @return PdoDb */ public function query($statement) { $res = $this->DB->query($statement); if ($res) { $this->Stmt = $res; $this->Sql = $statement; return $this; } $this->errorMessage(); } /** * 序列化一次数据 * @return mixed */ public function fetchOne() { return $this->Stmt->fetch(); } /** * 序列化所有数据 * @return array */ public function fetchAll() { return $this->Stmt->fetchAll(); } /** * 最后添加的id * @return string */ public function lastId() { return $this->DB->lastInsertId(); } /** * 影响的行数 * @return int */ public function affectRows() { return $this->Stmt->rowCount(); } /** * 预备语句 * @param string $statement * @return PdoDb */ public function prepare($statement) { $res = $this->DB->prepare($statement); if ($res) { $this->Stmt = $res; $this->Sql = $statement; return $this; } $this->errorMessage(); } /** * 绑定数据 * @param array $array * @return PdoDb */ public function bindArray($array) { foreach ($array as $k => $v) { if (is_array($v)) { //array的有效结构 array('value'=>xxx,'type'=>PDO::PARAM_XXX) $this->Stmt->bindValue($k + 1, $v['value'], $v['type']); } else { $this->Stmt->bindValue($k + 1, $v, PDO::PARAM_STR); } } return $this; } /** * 执行预备语句 * @return bool */ public function execute() { if ($this->Stmt->execute()) { return true; } $this->errorMessage(); } /** * 开启事务 * @return bool */ public function beginTransaction() { return $this->DB->beginTransaction(); } /** * 执行事务 * @return bool */ public function commitTransaction() { return $this->DB->commit(); } /** * 回滚事务 * @return bool */ public function rollbackTransaction() { return $this->DB->rollBack(); } /** * 抛出错误 * @throws Error * @return void */ public function errorMessage() { $msg = $this->DB->errorInfo(); throw new Error('数据库错误:' . $msg[2]); } //--------------------- /** * 单例实例 * @var PdoDb */ protected static $_instance; /** * 默认数据库 * @static * @param array $config * @return PdoDb */ public static function instance($config) { if (!self::$_instance instanceof PdoDb) { self::$_instance = new PdoDb($config); self::$_instance->connect(); } return self::$_instance; } //---------------------- /** * 获取PDO支持的数据库 * @static * @return array */ public static function getSupportDriver(){ return PDO::getAvailableDrivers(); } /** * 获取数据库的版本信息 * @return array */ public function getDriverVersion(){ $name = $this->DB->getAttribute(PDO::ATTR_DRIVER_NAME); return array($name=>$this->DB->getAttribute(PDO::ATTR_CLIENT_VERSION)); } }
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article
Assassin's Creed Shadows: Seashell Riddle Solution
3 weeks ago
By DDD
What's New in Windows 11 KB5054979 & How to Fix Update Issues
2 weeks ago
By DDD
Where to find the Crane Control Keycard in Atomfall
3 weeks ago
By DDD
Saving in R.E.P.O. Explained (And Save Files)
1 months ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics
CakePHP Tutorial
1385
52

