Discuz x2.5 x3.0如何配置连接多个数据库同时使用
修改 conf/config_global.php
$_config['db']['map']=array('表名1'=>'2','表名2'=>'2);
//这里面2对应映射配置
$_config['db']['2']['dbhost'] = 'localhost';
$_config['db']['2']['dbuser'] = 'root';
$_config['db']['2']['dbpw'] = '';
$_config['db']['2']['dbcharset'] = 'gbk';
$_config['db']['2']['pconnect'] = '0';
$_config['db']['2']['dbname'] = '数据库名';
//这个参数其实没有用,要照我随后的修改就可以用了
$_config['db']['2']['tablepre'] = 'cdb_';
修改 source/class/db/db_driver_mysql.php
找到 table_name
函数
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];
//增加了这一句
return $this->config[$id]['tablepre'].$tablename;
} else {
$this->curlink = $this->link[1];
}
return $this->tablepre.$tablename;
}
执行连接
print_r(DB::fetch_first("SELECT * FROM ".DB::table('nav')." limit 1"));