Discuz多个数据库配置,跨库调用其他网站数据
实现思路
$_config['db']['1']['dbhost'] = 'localhost'; $_config['db']['1']['dbuser'] = 'root'; $_config['db']['1']['dbpw'] = 'root'; $_config['db']['1']['dbcharset'] = 'gbk'; $_config['db']['1']['pconnect'] = '0'; $_config['db']['1']['dbname'] = 'wh7d'; $_config['db']['1']['tablepre'] = 'pre_'; // 新增外链数据库部分(下面的2就是第2个服务器,当然也可以有3、4、5个服务器) $_config['db']['2']['dbhost'] = 'localhost'; // 如果是同一个服务器,就填写localhost,不在一个服务器,填写具体的服务器IP地址 $_config['db']['2']['dbuser'] = 'rieueo'; // 用户名 $_config['db']['2']['dbpw'] = 'dfsadfasd'; // 密码 $_config['db']['2']['dbcharset'] = 'gbk'; // 这里的编码注意下,要跟第一个服务器的编码保持一致,即使你的第二个数据是UTF8编码这里也填写GBK $_config['db']['2']['pconnect'] = '0'; $_config['db']['2']['dbname'] = 'dfadsf'; // 数据库名称 $_config['db']['2']['tablepre'] = 'ree_'; // 数据库前缀 $_config['db']['map']['ecms_az'] = 2; // 这里注意下,ecms_az是你要调用数据库的表名,2就是从第二个数据库调用,这里可以填写多个表,一个表一行就行 $_config['db']['slave'] = ''; $_config['db']['common']['slave_except_table'] = '';
2、修改source\class\db\db_driver_mysql.php,大概102行开始,看下面注释,需要添加一句话:
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;
}3、具体调用数据:
<!--{eval $new_tips = DB::fetch_all("SELECT * FROM ".DB::table('ecms_az')." order by newstime desc LIMIT 18"); }-->
<!--{loop $new_tips $value}-->
<li>
<a target="_blank" href="{$value['titleurl']}"> <img src="{$value['titlepic']}" alt="{$value['title']}">
<p class="item-tit">{$value['title']}</p>
</a>
</li>
<!--{/loop}-->目前测试,基于X3.2、X3.3、X3.4都可以完美调用数据。
留言评论
暂无留言