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

封裝PDO操作數(shù)據(jù)庫

1,創(chuàng)建文件MysqlPdo.class.php文件用來封裝PDO操作數(shù)據(jù)庫

具體代碼如下所示:

<?php
class MysqlPdo{
    private $dbConfig=array(
        'db'=>'mysql',
        'host'=>'localhost',
        'port'=>'3306',
        'user'=>'root',
        'pwd'=>'',
        'charset'=>'utf8',
        'dbname'=>''
    );
    private static $instance;  //單例模式
    private $db;   //PDO實(shí)例
    private $data=array(); //操作數(shù)據(jù)
    private function __construct($params)
    {
        $this->dbConfig=array_merge($this->dbConfig,$params);
        $this->connect();
    }
    //連接服務(wù)器
    private function connect(){
        //mysql:host=localhost
        //mysql:host:localhost;port=3306;dbname=php;charset=utf-8
        $dsn="{$this->dbConfig['db']}:host={$this->dbConfig['host']};port={$this->dbConfig['port']};dbname={$this->dbConfig['dbname']};charset={$this->dbConfig['charset']}}";
        try{
            //實(shí)例化PDO
            $this->db=new PDO($dsn,$this->dbConfig['user'],$this->dbConfig['pwd']);
        }catch (PDOException $exception){
            die("數(shù)據(jù)庫連接失敗");
        }
    }
    public static function getInstance($params=array()){
        if(!self::$instance instanceof self){
            self::$instance=new self($params);
        }
        return self::$instance; //返回對(duì)象
    }
    //私有化克隆,防止外部調(diào)用clone $對(duì)象 生成新的對(duì)象,因?yàn)槭菃卫J?    private function __clone()
    {
        // TODO: Implement __clone() method.
    }
    //通過預(yù)處理方式執(zhí)行sql
    public function query($sql,$batch=false){
        $data=$batch?$this->data:array($this->data);
        $this->data=array();
        //通過預(yù)處理方式執(zhí)行SQL
        $stmt=$this->db->prepare($sql);
        foreach($data as $v){
            if($stmt->execute($v)===false){
                die("數(shù)據(jù)庫PDO預(yù)處理操作失敗");
            }
        }
        return $stmt;
    }
    public function data($data){
        $this->data=$data;
        return $this; //返回對(duì)象自身用于連貫操作
    }
    //取得一行結(jié)果
    public function fetchRow($sql){
        return $this->query($sql)->fetch(PDO::FETCH_ASSOC);//返回索引數(shù)組
    }
    //取得多行結(jié)果
    public function fetchAll($sql){
        return $this->query($sql)->fetchAll(PDO::FETCH_ASSOC);
    }
}

1,本封裝類采用單例模式,私有化了構(gòu)造方法和克隆方法,保證只能有一個(gè)實(shí)例化對(duì)象,節(jié)約數(shù)據(jù)庫連接資源,

2,在每次創(chuàng)建數(shù)據(jù)庫連接對(duì)象時(shí)必須調(diào)用getInstance(),在getInstance里傳入數(shù)據(jù)庫配置信息用于在實(shí)例化對(duì)象的時(shí)候自動(dòng)調(diào)用構(gòu)造函數(shù)使傳入的數(shù)據(jù)庫配置信息與原來的信息進(jìn)行合并,方便擴(kuò)展其他數(shù)據(jù)庫的連接,更換數(shù)據(jù)庫也只需要更改配置信息即可

3,通過預(yù)處理方式執(zhí)行sql語句

微信圖片_20180306141145.png

4,連貫操作的使用

微信圖片_20180306141535.png

5,處理查詢的結(jié)果集

微信圖片_20180306141909.png

以上便是對(duì)數(shù)據(jù)庫類的封裝處理

繼續(xù)學(xué)習(xí)
||
<?php echo "封裝類的處理";
提交重置代碼
章節(jié)
筆記
提問
課件
反饋
捐贈(zèng)

文章管理系統(tǒng)

  • 推薦課程
  • 評(píng)論
  • 問答
  • 筆記
  • 課件下載

aspirant

是撒奧所大所多·阿薩德大撒所

6年前    添加回復(fù) 1

Ponzio

charset={$this->dbConfig['charset']}}"; 多個(gè)}

4年前    添加回復(fù) 0

課件暫不提供下載,工作人員正在整理中,后期請(qǐng)多關(guān)注該課程~