首页 > 数据库技术 > 详细

Thinkphp5多数据库切换

时间:2019-02-28 10:08:10      阅读:164      评论:0      收藏:0      [点我收藏+]

在项目开发中需要Thinkphp5读取多个数据库的数据,本文详细介绍Thinkphp5多数据库切换

一、在database.php配置默认数据库连接

‘type‘           => ‘mysql‘,
‘hostname‘       => ‘服务器IP地址‘,
‘database‘       => ‘数据库名‘,
‘username‘       => ‘用户名‘,
‘password‘       => ‘密码‘,
‘hostport‘       => ‘数据库端口‘,

二、在config.php配置第二个数据库连接

‘DB_Config_1‘=>[
‘type‘           => ‘mysql‘,
‘hostname‘       => ‘服务器IP地址‘,
‘database‘       => ‘数据库名‘,
‘username‘       => ‘用户名‘,
‘password‘       => ‘密码‘,
‘hostport‘       => ‘数据库端口‘
],

三、多数据库使用

//默认数据库读取数据
$test = Db::name("test")->select();
//第二个数据库读取数据
$test1=Db::connect("DB_Config_1")->name("test")->select();

在项目开发中需要Thinkphp5读取多个数据库的数据,本文详细介绍Thinkphp5多数据库切换

一、在database.php配置默认数据库连接

‘type‘           => ‘mysql‘,
‘hostname‘       => ‘服务器IP地址‘,
‘database‘       => ‘数据库名‘,
‘username‘       => ‘用户名‘,
‘password‘       => ‘密码‘,
‘hostport‘       => ‘数据库端口‘,

二、在config.php配置第二个数据库连接

‘DB_Config_1‘=>[
‘type‘           => ‘mysql‘,
‘hostname‘       => ‘服务器IP地址‘,
‘database‘       => ‘数据库名‘,
‘username‘       => ‘用户名‘,
‘password‘       => ‘密码‘,
‘hostport‘       => ‘数据库端口‘
],

三、多数据库使用

//默认数据库读取数据
$test = Db::name("test")->select();
//第二个数据库读取数据
$test1=Db::connect("DB_Config_1")->name("test")->select();

Thinkphp5多数据库切换

原文:https://www.cnblogs.com/zxf100/p/10448133.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!