ITPub博客

首页 > IT职业 > IT职场 > Thinkphp5多数据库切换

Thinkphp5多数据库切换

IT职场 作者:ghost_hand 时间:2021-09-09 10:27:32 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();

分类: 技术问题

 作者:下页、再停留

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


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1059/viewspace-2822201/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 富人赚钱靠什么
请登录后发表评论 登录
全部评论

注册时间:2019-04-15

  • 博文量
    81
  • 访问量
    444