国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 PHP源碼 PHP PDO database operation class

PHP PDO database operation class

Nov 08, 2016 pm 02:17 PM

A simple PDO class package. . For learning and communication only

PdoDb database class

<?php
/**
 * @throws Error
 * PDO數(shù)據(jù)庫
 */
 
class PdoDb extends DatabaseAbstract
{
    /**
     * PDO實例
     * @var PDO
     */
    protected $DB;
    /**
     * PDO準(zhǔn)備語句
     * @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;
 
    /**
     * 構(gòu)造函數(shù)
     * @param array $config
     */
    public function __construct($config)
    {
        $this->Config = $config;
    }
 
    /**
     * 連接數(shù)據(jù)庫
     * @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;]);
        //默認(rèn)把結(jié)果序列化成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;
    }
 
    /**
     * 執(zhí)行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();
    }
 
    /**
     * 序列化一次數(shù)據(jù)
     * @return mixed
     */
    public function fetchOne()
    {
        return $this->Stmt->fetch();
    }
 
    /**
     * 序列化所有數(shù)據(jù)
     * @return array
     */
    public function fetchAll()
    {
        return $this->Stmt->fetchAll();
    }
 
    /**
     * 最后添加的id
     * @return string
     */
    public function lastId()
    {
        return $this->DB->lastInsertId();
    }
 
    /**
     * 影響的行數(shù)
     * @return int
     */
    public function affectRows()
    {
        return $this->Stmt->rowCount();
    }
 
    /**
     * 預(yù)備語句
     * @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();
    }
 
    /**
     * 綁定數(shù)據(jù)
     * @param array $array
     * @return PdoDb
     */
    public function bindArray($array)
    {
        foreach ($array as $k => $v) {
            if (is_array($v)) {
                //array的有效結(jié)構(gòu) 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;
    }
 
    /**
     * 執(zhí)行預(yù)備語句
     * @return bool
     */
    public function execute()
    {
        if ($this->Stmt->execute()) {
            return true;
        }
        $this->errorMessage();
    }
 
    /**
     * 開啟事務(wù)
     * @return bool
     */
    public function beginTransaction()
    {
        return $this->DB->beginTransaction();
    }
 
    /**
     * 執(zhí)行事務(wù)
     * @return bool
     */
    public function commitTransaction()
    {
        return $this->DB->commit();
    }
 
    /**
     * 回滾事務(wù)
     * @return bool
     */
    public function rollbackTransaction()
    {
        return $this->DB->rollBack();
    }
 
    /**
     * 拋出錯誤
     * @throws Error
     * @return void
     */
    public function errorMessage()
    {
        $msg = $this->DB->errorInfo();
        throw new Error(&#39;數(shù)據(jù)庫錯誤:&#39; . $msg[2]);
    }
 
    //---------------------
    /**
     * 單例實例
     * @var PdoDb
     */
    protected static $_instance;
 
    /**
     * 默認(rèn)數(shù)據(jù)庫
     * @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支持的數(shù)據(jù)庫
     * @static
     * @return array
     */
    public static function getSupportDriver(){
        return PDO::getAvailableDrivers();
    }
    /**
     * 獲取數(shù)據(jù)庫的版本信息
     * @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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1502
276