After the CodeIgniter Base URL Configuration now we move on the CodeIgniter Database Configuration. As we configure base URL for the whole application we need to configure the database to utilize in the application. It is not like base URL if you will not configure database you will not be able to utilize DB in the whole application.

Even that we need to configure the database for the different environments such as production or development version. Following is the default code of database configuration file.

$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
  'dsn'	=> '',
  'hostname' => 'localhost',
  'username' => '',
  'password' => '',
  'database' => '',
  'dbdriver' => 'mysqli',
  'dbprefix' => '',
  'pconnect' => FALSE,
  'db_debug' => (ENVIRONMENT !== 'production'),
  'cache_on' => FALSE,
  'cachedir' => '',
  'char_set' => 'utf8',
  'dbcollat' => 'utf8_general_ci',
  'swap_pre' => '',
  'encrypt' => FALSE,
  'compress' => FALSE,
  'stricton' => FALSE,
  'failover' => array(),
  'save_queries' => TRUE
);

In the above code, you can observe it is an array. You can leave some indexes to their default values. But the following options are compulsory to be used to configure a database.

  • hostname – If you are using this application in your local system then you have to place “localhost” as a value. Even if you are hosting this for production you can set hostname as localhost or define IP of your server.
'hostname' => 'localhost'

Or

'hostname' => '192.68.3.28'

Or

'hostname' => 'tutorialpace.com'
  • username – It is the name of the database user which you will assign privileges to communicate with the database.
'username' => 'tutorialpace_user'
  • password – It is a password of your username which you have assign privileges to communicate with DB.
'password' => 'xsnjvn1!jujnmxc?q'
  • database – It is the name of your database which you have created to store all the records for your application that exchange.
'database' => 'tutoriapace_database'
  • dbdriver – Here you can set the type of database which you are using. By default editor to the MySQLi. But you can also configure it to MySQL, ODBC, MS SQL or Postgre SQL.
'dbdriver' => 'mysqli'

Setup Codeigniter Multiple Databases

As we have discussed at the beginning of this tutorial that we can configure multiple database instances for the different environment of the application. In the above code, you will observe that in “$db[‘default’]” default word indicate the key of this array.

Which is storing the configuration of the database. If you need multiple instances of a database having different configurations then you just need to place different words within this array.

In the following example, we are creating two database instance by implementing the discussed methodology.

/* == Default Instance of CodeIgniter Database == */

$db['default'] = array(
  'dsn'	=> '',
  'hostname' => 'localhost',
  'username' => 'tutorialpace_admin',
  'password' => 'tutorialpace_admin',
  'database' => 'tutorialpace_admin_db',
  'dbdriver' => 'mysqli',
  'dbprefix' => '',
  'pconnect' => FALSE,
  'db_debug' => (ENVIRONMENT !== 'production'),
  'cache_on' => FALSE,
  'cachedir' => '',
  'char_set' => 'utf8',
  'dbcollat' => 'utf8_general_ci',
  'swap_pre' => '',
  'encrypt' => FALSE,
  'compress' => FALSE,
  'stricton' => FALSE,
  'failover' => array(),
  'save_queries' => TRUE
);

/* == Custom Instance of CodeIgniter Database == */

$db['testing'] = array(
  'dsn'	=> '',
  'hostname' => 'localhost',
  'username' => 'tutorialpace_tester',
  'password' => 'tutorialpace_tester',
  'database' => 'tutorialpace_tester_db',
  'dbdriver' => 'mysqli',
  'dbprefix' => '',
  'pconnect' => FALSE,
  'db_debug' => (ENVIRONMENT !== 'production'),
  'cache_on' => FALSE,
  'cachedir' => '',
  'char_set' => 'utf8',
  'dbcollat' => 'utf8_general_ci',
  'swap_pre' => '',
  'encrypt' => FALSE,
  'compress' => FALSE,
  'stricton' => FALSE,
  'failover' => array(),
  'save_queries' => TRUE
);

In the above code example, we have created two instances of database one for the default application environment and second for the test application environment.

You can simply Switch between database instances with the help of “$active_group” value. Such as if you want to switch from default database instance to test the instance of the database then you will code like below.

$active_group = 'tester';