Reputation: 307
I am new to codeigniter, and I want to connect to two databases to get the values in the same file. I googled around a bit and found that we can do that by using:
$dsn1 = 'dbdriver://user:pass@ip/db1';
$dsn2 = 'dbdriver://user:pass@ip/db2';
$DB1 = $this->load->database($dsn1,TRUE);
$DB2 = $this->load->database($dsn2,TRUE);
Now this gives the error saying that
dbdriver_driver.php
file is not present.
This is a fresh installation.
And I will post the contents of the database folder shortly.
Edit: There is no drivers folder in the database directory
Edit2: It says no such file or directory, and that is true. Should I install addons for this to work or did I somewhere make a mistake or forget a part during installation of codeigniter
Edit 3: The database.php file:
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = FALSE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
The hostname, username and password are set as well.
Upvotes: 0
Views: 157
Reputation: 1461
You're mixing apples with potatoes here. This is how your config/database.php
should look like:
$active_group = 'db1'; // the default db connection.
$active_record = TRUE;
$db['db1']['hostname'] = 'localhost';//or ip
$db['db1']['username'] = 'username';
$db['db1']['password'] = 'pass';
$db['db1']['database'] = 'db_name';
$db['db1']['dbdriver'] = 'mysql';
$db['db1']['dbprefix'] = '';
$db['db1']['pconnect'] = TRUE;
$db['db1']['db_debug'] = TRUE;
$db['db1']['cache_on'] = FALSE;
$db['db1']['cachedir'] = '';
$db['db1']['char_set'] = 'utf8';
$db['db1']['dbcollat'] = 'utf8_general_ci';
$db['db1']['swap_pre'] = '';
$db['db1']['autoinit'] = TRUE;
$db['db1']['stricton'] = FALSE;
// the second db connection configuration
$db['db2']['hostname'] = 'localhost';//or ip
$db['db2']['username'] = 'username';
$db['db2']['password'] = 'pass';
$db['db2']['database'] = 'db_name';
$db['db2']['dbdriver'] = 'mysql';
$db['db2']['dbprefix'] = '';
$db['db2']['pconnect'] = TRUE;
$db['db2']['db_debug'] = TRUE;
$db['db2']['cache_on'] = FALSE;
$db['db2']['cachedir'] = '';
$db['db2']['char_set'] = 'utf8';
$db['db2']['dbcollat'] = 'utf8_general_ci';
$db['db2']['swap_pre'] = '';
$db['db2']['autoinit'] = TRUE;
$db['db2']['stricton'] = FALSE;
So you have two database connections configured. Now, you must use them like this, inside controllers or model functions:
$DB1 = $this->load->database('db1',TRUE);
$DB2 = $this->load->database('db2',TRUE);
EDIT:
Your DSN variable should have everything needed to establish a connection, it will not look inside config/database.php
file. I suppose you diddn't actually change 'dbdriver' key-word in $dsn1 = 'dbdriver://user:pass@ip/db1;
string to the actual db engine name (?), like 'mysql', 'msqli', 'pdo' or whatever engine you use.
So if you write 'dbdriver', CI expects to find a folder named 'dbdriver'(with that exact name) inside 'system/database/drivers/' directory, and a php file named 'dbdriver_driver.php'. If you write 'mysql', it will find the mysql driver file : 'system/database/drivers/mysql/mysql_driver.php'. The other configuration variables, like pconnect, db_debug or whatever, should be mentioned as a query string:
$dsn1 = 'mysql://user:pass@ip/db1?pconnect=true&db_debug=false&etc...';
$dsn2 = 'pdo://user:pass@ip/db1?pconnect=true&db_debug=false&etc...';
Upvotes: 1