我正在研究安装在localhost和web服务器上的codeigniter应用程序和应用程序,但实际上我希望数据应该使用localhost插入localhost数据库和Web服务器数据库?
如何在database.php文件中连接Web服务器数据库以及如何在CI_Controller函数中使用Web服务器连接?
答案 0 :(得分:0)
你可以查看这篇文章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);
}
答案 1 :(得分:0)
你可以这样做
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
}