首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >codeigniter使用localhost将数据插入两个数据库?

问codeigniter使用localhost将数据插入两个数据库?
EN

Stack Overflow用户
提问于 2016-10-04 02:53:38
回答 2查看 980关注 0票数 0

我正在开发codeigniter应用程序和安装在localhost和web服务器上的应用程序,但实际上我希望使用localhost将数据插入到localhost数据库和web服务器数据库?

如何在database.php文件中连接web服务器数据库,以及如何在CI_Controller函数中使用web服务器连接?

EN

回答 2

Stack Overflow用户

发布于 2016-10-04 03:33:42

你可以看到这个帖子Codeigniter - multiple database connections,但基本上是这样的,把这个添加到database.php

代码语言:javascript
复制
$db['otherdb']['hostname'] = "localhost";
$db['otherdb']['username'] = "root";
$db['otherdb']['password'] = "";
$db['otherdb']['database'] = "other_database_name";
$db['otherdb']['dbdriver'] = "mysql";
$db['otherdb']['dbprefix'] = "";
$db['otherdb']['pconnect'] = TRUE;
$db['otherdb']['db_debug'] = FALSE;
$db['otherdb']['cache_on'] = FALSE;
$db['otherdb']['cachedir'] = "";
$db['otherdb']['char_set'] = "utf8";
$db['otherdb']['dbcollat'] = "utf8_general_ci";
$db['otherdb']['swap_pre'] = "";
$db['otherdb']['autoinit'] = TRUE;
$db['otherdb']['stricton'] = FALSE;

你可以像这样从模型中调用它

代码语言:javascript
复制
function my_model_method()
{
  $otherdb = $this->load->database('otherdb', TRUE); // the TRUE paramater tells CI that you'd like to return the database object.

  $query = $otherdb->select('first_name, last_name')->get('person');
  var_dump($query);
}
票数 0
EN

Stack Overflow用户

发布于 2016-10-04 04:36:53

你可以做到的

代码语言:javascript
复制
function my_model_method()
{
$otherdb = $this->load->database('otherdb', TRUE);
$data= array('name'=>'John');
$database1=$this->db->insert('students', $data); //default db
$database2 = $otherdb->insert('students',$data); //the other db
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39838496

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档