我正在开发codeigniter应用程序和安装在localhost和web服务器上的应用程序,但实际上我希望使用localhost将数据插入到localhost数据库和web服务器数据库?
如何在database.php文件中连接web服务器数据库,以及如何在CI_Controller函数中使用web服务器连接?
发布于 2016-10-04 03:33:42
你可以看到这个帖子Codeigniter - multiple database connections,但基本上是这样的,把这个添加到database.php
$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;你可以像这样从模型中调用它
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);
}发布于 2016-10-04 04:36:53
你可以做到的
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
}https://stackoverflow.com/questions/39838496
复制相似问题