config 配置文件添加词句
<?php return array( // 示例 mysql://root:88888888@192.168.0.152:3306/jshs
'DB2' => 'mysql://username:password@ip:port/datebase',
);
Controll 代码如下
/** * # +======================================================================== * # | - @name 连接多个数据库 * # | - @author cq <just_leaf@foxmail.com> * # | - @copyright zmtek 2018-11-09 * # +------------------------------------------------------------------------ * # | - 1.192.168.0.133 是项目主数据库 * # | - 2.192.168.0.152 是配置之后的数据库 * # | - 1.M('表格','前缀','配置名称') * # +======================================================================== */ public function Db2() { # 数据库(192.168.0.133) $user = M('user') -> order('id desc') -> find(); var_dump($user); # 数据库(192.168.0.152) $user = M('user','js_','DB2') -> order('id desc') -> find(); var_dump($user); }