复制 /application/database.php -> database2.php, 配置第二个数据库信息到 database2.php
在 /application/config.php 导入 $db_config_cards = require_once('database2.php');
在 /application/config.php 参数加一项 -> 'db_config_cards' => $db_config_cards;(如果有第三个数据,继续在下面添加配置 'db_config_cards3' => $db_config_cards3)
用第二个数据库连接创建一个model试试,假设第二个数据库表里有个tp5_news_content表
创建模型文件/application/index/model/NewsContent.php 代码如下
namespace app\Index\model;
use think\model;
class NewsContent extends Model
{
// 指定连接第二个数据库
protected $connection = 'db_config_cards';
// 这个配置是解决 toArray() 方法报错的
protected $resultSetType = 'collection';
}
在控制器里 使用NewsContent
public function test()
{
$news = new \app\Index\model\NewsContent();
$list = $news->select()->toArray();
print_r($list);
}