In Applications/config folder open the database.php file.
You will find the default database connection code. Which is need to to be
replaced with your settings. Like database name, hostname, username, password,
etc… And ‘$active_group’ variable is set with ‘default’ value. You can replace
them all but only if you are sure about them. The ‘Record set’ code would be like this,
$active_group = 'default';
$active_record
= TRUE;
$db['default']['hostname']
= 'localhost';
$db['default']['username']
= 'root';
$db['default']['password']
= '';
$db['default']['database']
= 'feedback';
$db['default']['dbdriver']
= 'mysql';
$db['default']['dbprefix']
= '';
$db['default']['pconnect']
= TRUE;
$db['default']['db_debug']
= TRUE;
$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;
If you want to connect to multiple databases at a time, you
can connect by Copying Record set and paste below the code and change the multi-dimensional
array key value ‘default’ to ‘new_conn’ i.e
$db['default'] ==> $db['comments’]
And as well as the database values. Like hostname, dbname,
username, pwd, …
Following is the new database connection array.
//================NEW COMMENTS
DATABASE ==========
$db['comments']['hostname']
= 'localhost';
$db['comments']['username']
= 'root';
$db['comments']['password']
= '';
$db['comments']['database']
= 'comments';
$db['comments']['dbdriver']
= 'mysql';
$db['comments']['dbprefix']
= '';
$db['comments']['pconnect']
= TRUE;
$db['comments']['db_debug']
= TRUE;
$db['comments']['cache_on']
= FALSE;
$db['comments']['cachedir']
= '';
$db['comments']['char_set']
= 'utf8';
$db['comments']['dbcollat']
= 'utf8_general_ci';
$db['comments']['swap_pre']
= '';
$db['comments']['autoinit']
= TRUE;
$db['comments']['stricton']
= FALSE;
Connection to
new database:
By default the CodeIgniter connection statement is
$this->load->database();
This code connects the default
database which is set following variables,
$active_group = 'default';
$active_record = TRUE;
For connecting to new database you need to specify the
database name along with this default connection statement. And you have to set
TRUE for $active_record variable, Following is the code snippet:
$a=$this->load->database('comments',TRUE);
First database() method arguments is database connection
array key value which will be set to ‘$active_group’ I.e comments which we
already defined below the default database connection array. And second argument
is TRUE/FALSE which will change ‘$active_record’ variable value.
This connection statement returns the connection results
which we storing into a variable. So we can query the database with that
variable. Following code is example query statements,
$res=$a->insert("comment ",$data);
$a->_error_message();
$a->distinct();
$a->select('name');
$query=$a->get("comment");
…
You may wish to connect to another new database then follow
the same steps to achieve.