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.

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.

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.

Tutorial Pace is Pakistan's first and only platform from where you can learn market demanding technologies right from developers. Tutorial Pace is developed by a developer for developers. We are working to maintain the quality of content and practical knowledge within our tutorials.