Home  >  Article  >  php教程  >  PHP PDO database operation class

PHP PDO database operation class

大家讲道理
大家讲道理Original
2016-11-08 14:17:021432browse

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(&#39;dsn&#39;=>xxx,&#39;name&#39;=>xxx,&#39;password&#39;=>xxx,&#39;option&#39;=>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[&#39;dsn&#39;], $this->Config[&#39;name&#39;], $this->Config[&#39;password&#39;], $this->Config[&#39;option&#39;]);
        //默认把结果序列化成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(&#39;value&#39;=>xxx,&#39;type&#39;=>PDO::PARAM_XXX)
                $this->Stmt->bindValue($k + 1, $v[&#39;value&#39;], $v[&#39;type&#39;]);
            } 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(&#39;数据库错误:&#39; . $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));
    }
 
}


Statement:
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