php pdo encapsulation class code (supports transactions)
Release: 2016-07-25 08:51:50
Original
1863 people have browsed it
-
- /**
- * PDO database
- * @copyright By GOOGLE
- */
- class pdo_db
- {
- /**
- * PDO instance
- * @var PDO
- */
- protected $_db;
- /**
- * PDO prepared statement
- * @var PDOStatement
- */
- protected $_stmt;
- /**
- * Last SQL statement
- * @var string
- */
- protected $_sql;
- /**
- * Configuration information $config=array('dsn'=>xxx,'name'=>xxx,'password'=>xxx,'option'=>xxx)
- * @var array
- */
- protected $_config;
- /**
- * Constructor
- * @param array $config
- */
- public function __construct($config)
- {
- $this->_config = $config;
- }
- /**
- * Connect to database
- * @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);
- $this->_db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
- //自己写代码捕获Exception
- $this->_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
- }
- /**
- * Disconnect
- * @return void
- */
- public function disConnect()
- {
- $this->_db = null;
- $this->_stmt = null;
- }
- /**
- * Execute sql and return the newly added id
- * @param string $statement
- * @return string
- */
- public function exec($statement)
- {
- if ($this->_db->exec($statement)){
- $this->_sql = $statement;
- return $this->lastId();
- }
- $this->errorMessage();
- }
- /**
- * Query sql
- * @param string $statement
- * @return pdo_db
- */
- public function query($statement)
- {
- $res = $this->_db->query($statement);
- if ($res){
- $this->_stmt = $res;
- $this->_sql = $statement;
- return $this;
- }
- $this->errorMessage();
- }
- /**
- * Serialize data once
- * @return mixed
- */
- public function fetchOne()
- {
- return $this->_stmt->fetch();
- }
- /**
- * Serialize all data
- * @return array
- */
- public function fetchAll()
- {
- return $this->_stmt->fetchAll();
- }
- /**
- * Last added id
- * @return string
- */
- public function lastId()
- {
- return $this->_db->lastInsertId();
- }
- /**
- * Number of rows affected
- * @return int
- */
- public function affectRows()
- {
- return $this->_stmt->rowCount();
- }
- /**
- * prepared statement
- * @param string $statement
- * @return pdo_db
- */
- public function prepare($statement)
- {
- $res = $this->_db->prepare($statement);
- if ($res){
- $this->_stmt = $res;
- $this->_sql = $statement;
- return $this;
- }
- $this->errorMessage();
- }
- /**
- * Bind data
- * @param array $array
- * @return pdo_db
- */
- 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;
- }
- /**
- * Execute prepared statements
- * @return bool
- */
- public function execute()
- {
- if ($this->_stmt->execute()){
- return true;
- }
- $this->errorMessage();
- }
- /**
- * Start transaction
- * @return bool
- */
- public function beginTransaction()
- {
- return $this->_db->beginTransaction();
- }
- /**
- * Execute transaction
- * @return bool
- */
- public function commitTransaction()
- {
- return $this->_db->commit();
- }
- /**
- * Rollback transaction
- * @return bool
- */
- public function rollbackTransaction()
- {
- return $this->_db->rollBack();
- }
- /**
- * Throws Error
- * @throws Error
- * @return void
- */
- public function errorMessage()
- {
- return;
- $msg = $this->_db->errorInfo();
- throw new Exception('数据库错误:' . $msg[2]);
- }
- //---------------------
- /**
- * Singleton instance
- * @var pdo_db
- */
- protected static $_instance;
- /**
- * Default database
- * @static
- * @param array $config
- * @return pdo_db
- */
- public static function instance($config)
- {
- if (!self::$_instance instanceof pdo_db){
- self::$_instance = new pdo_db($config);
- self::$_instance->connect();
- }
- return self::$_instance;
- }
- //----------------------
- /**
- * Get the database supported by PDO
- * @static
- * @return array
- */
- public static function getSupportDriver(){
- return PDO::getAvailableDrivers();
- }
- /**
- * Get the version information of the database
- * @return array
- */
- public function getDriverVersion(){
- $name = $this->_db->getAttribute(PDO::ATTR_DRIVER_NAME);
- return array($name=>$this->_db->getAttribute(PDO::ATTR_CLIENT_VERSION));
- }
- }
复制代码
|
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
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31