thinkphp数据库切换;
1:thinkphp3.1数据库切换在hone/conf/config.php
'DB_CONFIG2' => 'mysql://root:1234@192.168.1.10:3306/dbname#utf8';
//控制器中使用
$dbo = M('table')->db(2,"DB_CONFIG2");
或者
M('table')->db(2,"mysql://root:1234@192.168.1.10:3306/dbname#utf8");
thinkphp3.2//模型中单独配置
protected $connection = 'mysql://root:1234@192.168.1.10:3306/daname#utf8';
//配置文件配置
DB_CONFIG2' => 'mysql://root:1234@192.168.1.10:3306/daname#utf8';
thinkphp5.0//模型中单独配置
protected $connection = [
'type' => 'mysql', // 数据库类型
'dsn' => '', // 数据库连接DSN配置
'hostname' => '127.0.0.1', // 服务器地址
'database' => 'thinkphp', // 数据库名
'username' => 'root', // 数据库用户名
'password' => '', // 数据库密码
'hostport' => '', // 数据库连接端口
'params' => [], // 数据库连接参数
'charset' => 'utf8', // 数据库编码默认采用utf8
'prefix' => 'think_', // 数据库表前缀
];
protected $table = 'table1';//设置表名
// 或者 protected $connection = 'mysql://root:1234@127.0.0.1:3306/thinkphp#utf8';
// 或者在config.php 配置DB_CONFIG2' => 'mysql://root:1234@192.168.1.10:3306/daname#utf8';
然后在模型中protected $connection = 'DB_CONFIG2';