溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

開發(fā)自己的框架——(二)數(shù)據(jù)庫工具類的封裝

發(fā)布時間:2020-07-29 04:56:41 來源:網(wǎng)絡(luò) 閱讀:600 作者:流體石頭 欄目:數(shù)據(jù)庫

為了讓框架的內(nèi)容與數(shù)據(jù)分離,我們把常用的類封裝到一個工具類中,當(dāng)用到這些方法時,就調(diào)用這個封裝好的類,能夠使代碼的復(fù)用性得到很大的提高。
首先,封裝數(shù)據(jù)庫相關(guān)操作,為了使封裝規(guī)范化,我們創(chuàng)建一個接口讓數(shù)據(jù)庫實現(xiàn)接口中的方法,數(shù)據(jù)庫使用PDO擴(kuò)展訪問數(shù)據(jù)。
數(shù)據(jù)庫接口類 
I_DAO.interface.php

<?php
interface I_DAO
{
        //查詢所有數(shù)據(jù)的功能
        public function getAll($sql='');
//    //查詢一條數(shù)據(jù)
        public function getRow($sql='');
//    //查詢一個字段的值
        public function getOne($sql='');
//    //執(zhí)行增刪改的功能
        public function exec($sql='');
//    (查詢的時候,返回的結(jié)果數(shù))
        public function resultRows();
//    //查詢執(zhí)行插入操作返回的主鍵的值
        public function lastInsertId();
//    //
        public function query($sql='');
//    //轉(zhuǎn)義引號、并包裹的
        public function escapeData($data='');
}

數(shù)據(jù)庫工具類中,對象只能通過靜態(tài)方法創(chuàng)建一個實例(單例模式),不能通過克隆和繼承創(chuàng)建對象,數(shù)據(jù)庫的連接信息通過數(shù)組傳遞到方法中,工具類中有查詢所有數(shù)據(jù)方法、查詢一條數(shù)據(jù)方法、獲得一個字段值的方法、實現(xiàn)增刪改方法、
返回結(jié)果數(shù)量的方法等。
數(shù)據(jù)庫操作工具類
DAOPDO.class.php

<?php

class DAOPDO implements I_DAO
{    
    private $host;
    private $dbname;
    private $user;
    private $pass;
    private $port;
    private $charset;
    
    //該屬性保存pdo對象
    private $pdo;
    
    //查詢語句返回的結(jié)果集數(shù)量
    private $resultRows;
    
    //私有屬性保存該該實例
    private static $instance;
    //私有的構(gòu)造方法
    private function __construct($option=array())
    {
        //初始化服務(wù)器的配置
        $this -> initOptions($option);
        //初始化PDO對象
        $this -> initPDO();
    }
    //私有的克隆方法
    private function __clone()
    {
        
    }
    //公共的靜態(tài)方法實例化單例對象
    public static function getSingleton($options=array())
    {
        if(!self::$instance instanceof self){
            //實例化
            self::$instance = new self($options);
        }
        return self::$instance;
    }
    //初始化服務(wù)器的配置
    private function initOptions($option)
    {
        $this -> host = isset($option['host'])?$option['host']:'';
        $this -> dbname = isset($option['dbname'])?$option['dbname']:'';
        $this -> user = isset($option['user'])?$option['user']:'';
        $this -> pass = isset($option['pass'])?$option['pass']:'';
        $this -> port = isset($option['port'])?$option['port']:'';
        $this -> charset = isset($option['charset'])?$option['charset']:'';
    }
    //初始化PDO對象
    private function initPDO()
    {
        $dsn = 
        "mysql:host=$this->host;dbname=$this->dbname;port=$this->port;charset=$this->charset";
        $this -> pdo = new PDO($dsn,$this->user,$this->pass);
    }
    //封裝pdostatement對象
    public function query($sql="")
    {    
        //返回pdo_statement對象
        return $this->pdo -> query($sql);
    }
    //查詢所有數(shù)據(jù)
    public function getAll($sql='')
    {
        $pdo_statement = $this->query($sql);
        $this->resultRows = $pdo_statement -> rowCount();
        if($pdo_statement==false){
            //輸出SQL語句的錯誤信息
            $error_info = $this->pdo-> errorInfo();
            $err_str = "SQL語句錯誤,具體信息如下:<br>".$error_info[2];
            echo $err_str;
            return false;
        }
        $result = $pdo_statement -> fetchAll(PDO::FETCH_ASSOC);
        return $result;
    }
    //查詢一條記錄
    public function getRow($sql='')
    {
        $pdo_statement = $this->query($sql);
        if($pdo_statement==false){
            //輸出SQL語句的錯誤信息
            $error_info = $this->pdo-> errorInfo();
            $err_str = "SQL語句錯誤,具體信息如下:<br>".$error_info[2];
            echo $err_str;
            return false;
        }
        $result = $pdo_statement -> fetch(PDO::FETCH_ASSOC);
        return $result;
    }
    //獲得一個字段的值
    public function getOne($sql='')
    {
        $pdo_statement = $this->query($sql);
        if($pdo_statement==false){
            //輸出SQL語句的錯誤信息
            $error_info = $this->pdo-> errorInfo();
            $err_str = "SQL語句錯誤,具體信息如下:<br>".$error_info[2];
            echo $err_str;
            return false;
        }
        //返回查詢的字段的值,我們在執(zhí)行sql語句之前就應(yīng)該明確查詢的是哪個字段,這樣fetchColumn就已經(jīng)知道查詢的字段值
        $result = $pdo_statement -> fetchColumn();
        return $result;
    }
    //實現(xiàn)非查詢的方法
    public function exec($sql='')
    {
        $result = $this->pdo -> exec($sql);
        //===為了區(qū)分 受影響的記錄數(shù)是0的情況
        if($result===false){
            $error_info = $this->pdo-> errorInfo();
            $err_str = "SQL語句錯誤,具體信息如下:<br>".$error_info[2];
            echo $err_str;
            return false;
        }
        return $result;
    }
    //查詢語句返回的結(jié)果數(shù)量
    public function resultRows()
    {
        return $this->resultRows;
    }
    //返回上次執(zhí)行插入語句返回的主鍵值
    public function lastInsertId()
    {
        return $this->pdo->lastInsertId();
    }
    //數(shù)據(jù)轉(zhuǎn)義并引號包裹
    public function escapeData($data='')
    {
        return $this->pdo->quote($data);
    }
}


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI