欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php多數(shù)據(jù)庫(kù)支持的應(yīng)用程序設(shè)計(jì)

 更新時(shí)間:2008年08月12日 12:26:47   作者:  
以前做PHP應(yīng)用,多數(shù)是單數(shù)據(jù)庫(kù)數(shù)據(jù)查詢和更新,頂多也是主從數(shù)據(jù)庫(kù)的支持,實(shí)現(xiàn)起來(lái)相對(duì)簡(jiǎn)單。主從數(shù)據(jù)庫(kù)的問(wèn)題在于,當(dāng)會(huì)話存儲(chǔ)在數(shù)據(jù)庫(kù)的時(shí)候,同步將可能出現(xiàn)問(wèn)題,也就是說(shuō)有可能出現(xiàn)會(huì)話的中斷。

首先以單例摸式創(chuàng)建DB類(lèi):
復(fù)制代碼 代碼如下:

<?php
if (!defined("DB_FETCH_ASSOC")) {
    define("DB_FETCH_ASSOC", 1);
}

if (!defined("DB_FETCH_ROW")) {
    define("DB_FETCH_ROW", 2);
}

if (!defined("DB_FETCH_ARRAY")) {
    define("DB_FETCH_ARRAY", 3);
}

if (!defined("DB_FETCH_DEFAULT")) {
    define("DB_FETCH_DEFAULT", DB_FETCH_ASSOC);
}
class DB {
    function DB($dsn, $db_key, $p_conn, $fetch_mode) {
        $this->dsn        = $dsn;
        $this->db_key     = $db_key;
        $this->sql        = '';
        $this->sqls       = array();
        $this->u_sqls     = array();
        $this->q_sqls     = array();
        $this->u_conn     = null;
        $this->q_conn     = null;
        $this->p_conn     = $p_conn;
        $this->fecth_mode = $fetch_mode;
        $this->query_num  = 0;
        $this->update_num = 0;
    }

    function &init(& $dsn, $db_key, $p_conn = false, $fetch_mode = DB_FETCH_DEFAULT) {
        static $db;
        $db_key = explode('.', $db_key);
        $db_key = "['" . implode("']['" , $db_key) . "']";
        eval('$flag = isset($db' . $db_key . ');');
        eval("\$db_info = \$dsn['db_info']" . $db_key . ";");
        if (!$flag) {
            $obj = new DB($db_info, $db_key, $p_conn, $fetch_mode);
            eval('$db' . $db_key . ' = $obj;');
            unset($obj);
        }
        return $db;
    }
}
$db = &DB::init($configs, 'blog.0');
print_r($db);
?>

  從上面打印結(jié)果可以看出,blog數(shù)據(jù)庫(kù)集群的第一組數(shù)據(jù)庫(kù)服務(wù)器被載入到$this->dsn中了。這個(gè)下面就是簡(jiǎn)單的數(shù)據(jù)COPY的主從服務(wù)器,所以可以使用隨機(jī)數(shù)來(lái)指定到某一臺(tái)服務(wù)器。以下是一個(gè)簡(jiǎn)單的隨機(jī)數(shù)實(shí)現(xiàn):
復(fù)制代碼 代碼如下:

class DB {
    //....

    function connectDB($type = "master") {
        if ($type == "master") {
            $db_host = $this->dsn["master"]["db_host"];
            $db_name = $this->dsn["master"]["db_name"];
            $db_user = $this->dsn["master"]["db_user"];
            $db_pass = $this->dsn["master"]["db_pass"];
            $this->u_conn = mysqli_connect($db_host, $db_user, $db_pass);
            $this->selectDB($db_name, $this->conn);
            if (!$this->u_conn) {
                $message = "Update DataBase Connect False : ($db_host, $db_user, ******) !";
                $this->error($message, 0);
            }
        } else {
            if (empty($_COOKIE[$_configs['cookie_prefix'] . 'db_no'])) {
                $db_no = array_rand($this->dsn["db_info"]["slave"]);
            } else {
                $db_no = $_COOKIE[$_configs['cookie_prefix'] . 'db_no'];
            }
            $db_info = $this->dsn["slave"][$db_no];
            $db_host = $db_info["db_host"];
            $db_name = $db_info["db_name"];
            $db_user = $db_info["db_user"];
            $db_pass = $db_info["db_pass"];
            $this->q_conn = mysqli_connect($db_host, $db_user, $db_pass);

            if (!$this->q_conn) {
                if (!$this->u_conn) {
                    $this->connectDB();
                }
                $this->q_conn = $this->u_conn;
                if (!$this->q_conn) {
                    $message = "Query DataBase Connect False : ($db_host, $db_user, ******) !";
                    $this->error($message, 0);
                }
            } else {
                $this->selectDB($db_name, $this->q_conn);
            }
        }
    }

    function selectDB($db_name, $conn) {
        if ($db_name != null) {
            if(! mysqli_select_db($conn, $db_name)) {
                $code = mysqli_errno($conn);
                $message = mysqli_error($conn);
                $this->error($message, $code);
            }
            return true;
        }
        return false;
    }

    function query($sql, $limit = 1, $quick = false) {
        if ($limit != null) {
            $sql = $sql . " LIMIT " . $limit;
        }
        $this->sqls[] = $sql;
        $this->q_sqls[] = $sql;
        $this->sql = $sql;

        if (empty($this->q_conn)) {
            $this->connectDB("slave");
        }
        $this->qrs = mysqli_query($this->q_conn, $sql, $quick ? MYSQLI_USE_RESULT : MYSQLI_STORE_RESULT);
        if (!$this->qrs) {
            $code = mysqli_errno($this->q_conn);
            $message = mysqli_error($this->q_conn);
            $this->error($message, $code);
        }
        $this->query_num++;
        return $this->qrs;
    }

    function update($sql) {
        $this->sql = $sql;
        $this->sqls[] = $this->sql;
        $this->u_sqls[] = $this->sql;
        if ($this->u_conn == null) {
            $this->connectDB("master");
        }

        $this->urs = mysqli_query($this->u_conn, $sql, MYSQLI_USE_RESULT);
        $this->update_num++;

        if (!$this->urs) {
            return false;
        } else {
            return true;
        }
    }
}

  至此,基本框架已經(jīng)出來(lái)了,來(lái)看看調(diào)用方法: <?php
// 連接到第一組會(huì)話服務(wù)器
$db = &DB::init($configs, 'session.0');
//  執(zhí)行一次查詢
$db['session'][0]->query('SELECT ...');

//  再次連接BLOG服務(wù)器
$db = &DB::init($configs, 'blog.1');
//  執(zhí)行一次更新
$db['blog'][1]->update('UPDATE ...');

//  再次調(diào)用會(huì)話更新
$db['session'][0]->update('INSERT ...');
?>

相關(guān)文章

  • php之curl設(shè)置超時(shí)實(shí)例

    php之curl設(shè)置超時(shí)實(shí)例

    這篇文章主要介紹了php中curl設(shè)置超時(shí)的方法,實(shí)例講述了curl中各種超時(shí)設(shè)置的方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2014-11-11
  • 談?wù)凱HP的輸入輸出流

    談?wù)凱HP的輸入輸出流

    談?wù)凱HP的輸入輸出流...
    2007-02-02
  • PHP調(diào)用MySQL的存儲(chǔ)過(guò)程的實(shí)現(xiàn)代碼

    PHP調(diào)用MySQL的存儲(chǔ)過(guò)程的實(shí)現(xiàn)代碼

    MySQL好像從5.0開(kāi)始才引入存儲(chǔ)過(guò)程,反正以前做應(yīng)用的時(shí)候從沒(méi)碰過(guò),不過(guò)現(xiàn)在因?yàn)橹饕鲀?nèi)部系統(tǒng)
    2008-08-08
  • 如何讓PHP編碼更加好看利于閱讀

    如何讓PHP編碼更加好看利于閱讀

    在本篇文章里小編給大家分享了關(guān)于如何讓PHP編碼更加好看利于閱讀的方法和習(xí)慣,需要的朋友們可以學(xué)習(xí)下。
    2019-05-05
  • PHP實(shí)現(xiàn)讀取Excel文件的記錄(一)

    PHP實(shí)現(xiàn)讀取Excel文件的記錄(一)

    這篇文章主要介紹了如何利用PHP讀取Excel文件的記錄,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定幫助,感興趣的可以跟隨小編了解一下
    2022-03-03
  • php mysql操作mysql_connect連接數(shù)據(jù)庫(kù)實(shí)例詳解

    php mysql操作mysql_connect連接數(shù)據(jù)庫(kù)實(shí)例詳解

    php操作數(shù)據(jù)庫(kù)首先必須連接到指定的數(shù)據(jù)庫(kù),連接數(shù)據(jù)庫(kù)可以使用PHP mysql_connect函數(shù),本文章向大家介紹mysql_connect函數(shù)的使用方法和實(shí)例,需要的朋友可以參考一下
    2016-12-12
  • php實(shí)現(xiàn)批量刪除掛馬文件及批量替換頁(yè)面內(nèi)容完整實(shí)例

    php實(shí)現(xiàn)批量刪除掛馬文件及批量替換頁(yè)面內(nèi)容完整實(shí)例

    這篇文章主要介紹了php實(shí)現(xiàn)批量刪除掛馬文件及批量替換頁(yè)面內(nèi)容的方法,涉及php文件與目錄的遍歷、查找以及字符串與數(shù)組的遍歷與替換操作相關(guān)技巧,適用于utf-8編碼環(huán)境,需要的朋友可以參考下
    2016-07-07
  • php搜索文件程序分享

    php搜索文件程序分享

    php文件查找程序,輸入一個(gè)路徑確定后會(huì)遍歷目錄下所有的文件和文件夾,通過(guò)遞歸可以找到文件夾下面的每一個(gè)文件,再通過(guò)文件名和輸入的關(guān)鍵字匹配,則可以查找到你想要的文件,需要的朋友可以參考下
    2015-10-10
  • 基于php編程規(guī)范(詳解)

    基于php編程規(guī)范(詳解)

    下面小編就為大家?guī)?lái)一篇基于php編程規(guī)范(詳解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-08-08
  • php中文字符截取防亂碼

    php中文字符截取防亂碼

    當(dāng)漢字被截?cái)鄷r(shí),根據(jù)編碼規(guī)則他總是要把后邊的其他字符拉過(guò)來(lái)一起作為漢字解釋?zhuān)@就是出現(xiàn)亂碼的原因。而值為0x81到0xff與0x00組合始終都顯示為“空” 根據(jù)這一特點(diǎn),在substr的結(jié)果后面補(bǔ)上一個(gè)chr(0),就可以防止出現(xiàn)亂碼了
    2008-03-03

最新評(píng)論