oded
oded

Reputation: 155

how to change default database in codeigniter

I want to change my deafult database in my controller, but it doesn't change.

i'm doing this:

$this->db->close();

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

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

Upvotes: 1

Views: 1954

Answers (1)

Zaragoli
Zaragoli

Reputation: 686

Try something like this:

config:

$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';

controller:

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

Upvotes: 2

Related Questions