php多数据库支持的应用程序设计
更新时间:2008年08月12日 12:26:47 作者:
以前做PHP应用,多数是单数据库数据查询和更新,顶多也是主从数据库的支持,实现起来相对简单。主从数据库的问题在于,当会话存储在数据库的时候,同步将可能出现问题,也就是说有可能出现会话的中断。
首先以单例摸式创建DB类:
复制代码 代码如下:
<?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);
?>
从上面打印结果可以看出,blog数据库集群的第一组数据库服务器被载入到$this->dsn中了。这个下面就是简单的数据COPY的主从服务器,所以可以使用随机数来指定到某一台服务器。以下是一个简单的随机数实现:
复制代码 代码如下:
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;
}
}
}
至此,基本框架已经出来了,来看看调用方法: <?php
// 连接到第一组会话服务器
$db = &DB::init($configs, 'session.0');
// 执行一次查询
$db['session'][0]->query('SELECT ...');
// 再次连接BLOG服务器
$db = &DB::init($configs, 'blog.1');
// 执行一次更新
$db['blog'][1]->update('UPDATE ...');
// 再次调用会话更新
$db['session'][0]->update('INSERT ...');
?>
相关文章
php mysql操作mysql_connect连接数据库实例详解
php操作数据库首先必须连接到指定的数据库,连接数据库可以使用PHP mysql_connect函数,本文章向大家介绍mysql_connect函数的使用方法和实例,需要的朋友可以参考一下2016-12-12
最新评论