安静
PHP技术博客

130419 discuz x2.0 多数据库链接

上次写过 discuz x2.5 现在写个2.0的

关于为什么要写 这个 实际上 dz 支持多数据库 只是相当得不友好

你可以通过 DB: 无限制的去操作下去 可是如果你的 跨数据库是影响 比如 帖子里的用户信息的时候 这就不足够的友好了 要来回切换数据库 不如新建1个实例

修改配置文件 config/config_global.php

$_config['db']['5']['dbhost'] = '192.168.1.2';
$_config['db']['5']['dbuser'] = 'root';
$_config['db']['5']['dbpw'] = '123';
$_config['db']['5']['dbcharset'] = 'utf8';
$_config['db']['5']['pconnect'] = '0';
$_config['db']['5']['dbname'] = 'op';
$_config['db']['5']['tablepre'] = 'op_';

修改文件 source/class/class_core.php

//在代码最下面增加2个新的函数
class db_mysql_bi
{
var $tablepre;
var $version = '';
var $querynum = 0;
var $slaveid = 0;
var $curlink;
var $link = array();
var $config = array();
var $sqldebug = array();
var $map = array();
function db_mysql_bi($config = array()) {
if(!empty($config)) {
$this->set_config($config);
}
}
function set_config($config) {
$this->config = &$config;
$this->tablepre = $config['2']['tablepre'];
if(!empty($this->config['map'])) {
$this->map = $this->config['map'];
}
}
function connect($serverid = 2) {
if(empty($this->config) || empty($this->config[$serverid])) {
$this->halt('config_db_not_found');
}
$this->link[$serverid] = $this->_dbconnect(
$this->config[$serverid]['dbhost'],
$this->config[$serverid]['dbuser'],
$this->config[$serverid]['dbpw'],
$this->config[$serverid]['dbcharset'],
$this->config[$serverid]['dbname'],
$this->config[$serverid]['pconnect']
);
$this->curlink = $this->link[$serverid];
}
function _dbconnect($dbhost, $dbuser, $dbpw, $dbcharset, $dbname, $pconnect) {
$link = null;
$func = empty($pconnect) ? 'mysql_connect' : 'mysql_pconnect';
if(!$link = @$func($dbhost, $dbuser, $dbpw, 1)) {
$this->halt('notconnect');
} else {
$this->curlink = $link;
if($this->version() > '4.1') {
$dbcharset = $dbcharset ? $dbcharset : $this->config[1]['dbcharset'];
$serverset = $dbcharset ? 'character_set_connection='.$dbcharset.', character_set_results='.$dbcharset.', character_set_client=binary' : '';
$serverset .= $this->version() > '5.0.1' ? ((empty($serverset) ? '' : ',').'sql_mode=\'\'') : '';
$serverset && mysql_query("SET $serverset", $link);
}
$dbname && @mysql_select_db($dbname, $link);
}
return $link;
}
function table_name($tablename) {
if(!empty($this->map) && !empty($this->map[$tablename])) {
$id = $this->map[$tablename];
if(!$this->link[$id]) {
$this->connect($id);
}
$this->curlink = $this->link[$id];
} else {
$this->curlink = $this->link[1];
}
return $this->tablepre.$tablename;
}
function select_db($dbname) {
return mysql_select_db($dbname, $this->curlink);
}
function fetch_array($query, $result_type = MYSQL_ASSOC) {
return mysql_fetch_array($query, $result_type);
}
function fetch_first($sql) {
return $this->fetch_array($this->query($sql));
}
function result_first($sql) {
return $this->result($this->query($sql), 0);
}
function query($sql, $type = '') {
if(defined('DISCUZ_DEBUG') && DISCUZ_DEBUG) {
$starttime = dmicrotime();
}
$func = $type == 'UNBUFFERED' && @function_exists('mysql_unbuffered_query') ?
'mysql_unbuffered_query' : 'mysql_query';
if(!($query = $func($sql, $this->curlink))) {
if(in_array($this->errno(), array(2006, 2013)) && substr($type, 0, 5) != 'RETRY') {
$this->connect();
return $this->query($sql, 'RETRY'.$type);
}
if($type != 'SILENT' && substr($type, 5) != 'SILENT') {
$this->halt('query_error', $sql);
}
}
if(defined('DISCUZ_DEBUG') && DISCUZ_DEBUG) {
$this->sqldebug[] = array($sql, number_format((dmicrotime() - $starttime), 6), debug_backtrace());
}
$this->querynum++;
return $query;
}
function affected_rows() {
return mysql_affected_rows($this->curlink);
}
function error() {
return (($this->curlink) ? mysql_error($this->curlink) : mysql_error());
}
function errno() {
return intval(($this->curlink) ? mysql_errno($this->curlink) : mysql_errno());
}
function result($query, $row = 0) {
$query = @mysql_result($query, $row);
return $query;
}
function num_rows($query) {
$query = mysql_num_rows($query);
return $query;
}
function num_fields($query) {
return mysql_num_fields($query);
}
function free_result($query) {
return mysql_free_result($query);
}
function insert_id() {
return ($id = mysql_insert_id($this->curlink)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0);
}
function fetch_row($query) {
$query = mysql_fetch_row($query);
return $query;
}
function fetch_fields($query) {
return mysql_fetch_field($query);
}
function version() {
if(empty($this->version)) {
$this->version = mysql_get_server_info($this->curlink);
}
return $this->version;
}
function close() {
return mysql_close($this->curlink);
}
function halt($message = '', $sql = '') {
require_once libfile('class/error');
discuz_error::db_error($message, $sql);
}
}
class DB_BI
{
function table($table) {
return DB_BI::_execute('table_name', $table);
}
function delete($table, $condition, $limit = 0, $unbuffered = true) {
if(empty($condition)) {
$where = '1';
} elseif(is_array($condition)) {
$where = DB_BI::implode_field_value($condition, ' AND ');
} else {
$where = $condition;
}
$sql = "DELETE FROM ".DB_BI::table($table)." WHERE $where ".($limit ? "LIMIT $limit" : '');
return DB_BI::query($sql, ($unbuffered ? 'UNBUFFERED' : ''));
}
function insert($table, $data, $return_insert_id = false, $replace = false, $silent = false) {
$sql = DB_BI::implode_field_value($data);
$cmd = $replace ? 'REPLACE INTO' : 'INSERT INTO';
$table = DB_BI::table($table);
$silent = $silent ? 'SILENT' : '';
$return = DB_BI::query("$cmd $table SET $sql", $silent);
return $return_insert_id ? DB_BI::insert_id() : $return;
}
function update($table, $data, $condition, $unbuffered = false, $low_priority = false) {
$sql = DB_BI::implode_field_value($data);
$cmd = "UPDATE ".($low_priority ? 'LOW_PRIORITY' : '');
$table = DB_BI::table($table);
$where = '';
if(empty($condition)) {
$where = '1';
} elseif(is_array($condition)) {
$where = DB_BI::implode_field_value($condition, ' AND ');
} else {
$where = $condition;
}
$res = DB_BI::query("$cmd $table SET $sql WHERE $where", $unbuffered ? 'UNBUFFERED' : '');
return $res;
}
function implode_field_value($array, $glue = ',') {
$sql = $comma = '';
foreach ($array as $k => $v) {
$sql .= $comma."`$k`='$v'";
$comma = $glue;
}
return $sql;
}
function insert_id() {
return DB_BI::_execute('insert_id');
}
function fetch($resourceid, $type = MYSQL_ASSOC) {
return DB_BI::_execute('fetch_array', $resourceid, $type);
}
function fetch_first($sql) {
DB_BI::checkquery($sql);
return DB_BI::_execute('fetch_first', $sql);
}
function result($resourceid, $row = 0) {
return DB_BI::_execute('result', $resourceid, $row);
}
function result_first($sql) {
DB_BI::checkquery($sql);
return DB_BI::_execute('result_first', $sql);
}
function query($sql, $type = '') {
DB_BI::checkquery($sql);
return DB_BI::_execute('query', $sql, $type);
}
function num_rows($resourceid) {
return DB_BI::_execute('num_rows', $resourceid);
}
function affected_rows() {
return DB_BI::_execute('affected_rows');
}
function free_result($query) {
return DB_BI::_execute('free_result', $query);
}
function error() {
return DB_BI::_execute('error');
}
function errno() {
return DB_BI::_execute('errno');
}
function _execute($cmd , $arg1 = '', $arg2 = '') {
static $db_bi;
if(empty($db_bi)) $db_bi = & DB_BI::object();
$res = $db_bi->$cmd($arg1, $arg2);
return $res;
}
function &object($db_biclass = 'db_mysql_bi') {
static $db_bi;
if(empty($db_bi)) $db_bi = new $db_biclass();
return $db_bi;
}
function checkquery($sql) {
static $status = null, $checkcmd = array('SEL'=>1, 'UPD'=>1, 'INS'=>1, 'REP'=>1, 'DEL'=>1);
if($status === null) $status = getglobal('config/security/querysafe/status');
if($status) {
$test = 1;
$cmd = strtoupper(substr(trim($sql), 0, 3));
if(isset($checkcmd[$cmd])) {
$test = DB_BI::_do_query_safe($sql);
} elseif(substr($cmd, 0, 2) === '/*') {
$test = -1;
}
if($test < 1) DB_BI::_execute('halt', 'security_error', $sql);
}
return true;
}
function _do_query_safe($sql) {
static $_CONFIG = null;
if($_CONFIG === null) {
$_CONFIG = getglobal('config/security/querysafe');
}
$sql = str_replace(array('\\\\', '\\\'', '\\"', '\'\''), '', $sql);
$mark = $clean = '';
if(strpos($sql, '/') === false && strpos($sql, '#') === false && strpos($sql, '-- ') === false) {
$clean = preg_replace("/'(.+?)'/s", '', $sql);
} else {
$len = strlen($sql);
$mark = $clean = '';
for ($i = 0; $i <$len; $i++) {
$str = $sql[$i];
switch ($str) {
case '\'':
if(!$mark) {
$mark = '\'';
$clean .= $str;
} elseif ($mark == '\'') {
$mark = '';
}
break;
case '/':
if(empty($mark) && $sql[$i+1] == '*') {
$mark = '/*';
$clean .= $mark;
$i++;
} elseif($mark == '/*' && $sql[$i -1] == '*') {
$mark = '';
$clean .= '*';
}
break;
case '#':
if(empty($mark)) {
$mark = $str;
$clean .= $str;
}
break;
case "\n":
if($mark == '#' || $mark == '--') {
$mark = '';
}
break;
case '-':
if(empty($mark)&& substr($sql, $i, 3) == '-- ') {
$mark = '-- ';
$clean .= $mark;
}
break;
default:
break;
}
$clean .= $mark ? '' : $str;
}
}
$clean = preg_replace("/[^a-z0-9_\-\(\)#\*\/\"]+/is", "", strtolower($clean));
if($_CONFIG['afullnote']) {
$clean = str_replace('/**/','',$clean);
}
if(is_array($_CONFIG['dfunction'])) {
foreach($_CONFIG['dfunction'] as $fun) {
if(strpos($clean, $fun.'(') !== false) return '-1';
}
}
if(is_array($_CONFIG['daction'])) {
foreach($_CONFIG['daction'] as $action) {
if(strpos($clean,$action) !== false) return '-3';
}
}
if($_CONFIG['dlikehex'] && strpos($clean, 'like0x')) {
return '-2';
}
if(is_array($_CONFIG['dnote'])) {
foreach($_CONFIG['dnote'] as $note) {
if(strpos($clean,$note) !== false) return '-4';
}
}
return 1;
}

实例 Discuz x2.0 多数据库调用

$db_bi= & DB_BI::object();
$db_bi->set_config($_G['config']['db']);
$db_bi->connect();
$userinfo = DB_BI::fetch_first("SELECT * from aj_db limit 10");
var_dump($userinfo);

最后福利 关于 2.0 文本CACHE用法

require_once libfile('function/cache');
writetocache('ceshi', getcachevars(array('userinfo' => $userinfo)));
unset($userinfo);
if(file_exists($testfile = DISCUZ_ROOT.'./data/cache/cache_ceshi.php')) {
@include $testfile;
}
var_dump($userinfo);
赞(0) 打赏
未经允许不得转载:AJ's Blog » 130419 discuz x2.0 多数据库链接
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏