Home > php教程 > PHP源码 > body text

PHP PDO database operation class

大家讲道理
Release: 2016-11-08 14:17:02
Original
1415 people have browsed it

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));
    }
 
}
Copy after login


source:php.cn
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!