如何在codeigniter中更改默认数据库

时间:2016-04-27 14:42:58

标签: php database codeigniter

我想在我的控制器中更改我的deafult数据库,但它不会改变。

我这样做:

$this->db->close();

$config['default']['database'] = "second";

$this->load->database($config);

1 个答案:

答案 0 :(得分:2)

尝试这样的事情:

配置:

$active_group = 'default';
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'test1';


//Another database connection.
$db['db2']['hostname'] = 'localhost';
$db['db2']['username'] = 'root';
$db['db2']['password'] = '';
$db['db2']['database'] = 'test2';

控制器:

// close the default connection
$this->db->close();
// connect to the other db
$this->db = $this->load->database('db2', true);