In every application, there is an important part of a database through which developers can connect an application to store users’ data and perform various functions in the database application such as inserting, updating, reading, and deleting the values. Before performing these functionalities, you have to set the database configuration in the CodeIgniter application. A CodeIgniter contains an inbuilt database.php file that allows you to set database connection values such as hostname, username, password, database, dbdriver name, etc. and this file is located at application/config/database.php.

Database Configuration CodeIgniter

Set the Default Database Configuration

The default structure of the database configuration is as follows:

There are the following key points available in the database.php file while connecting with the database configuration:

  1. It specifies the host location of your database. For example- localhost or IP address
  2. It defines the username of your database, such as root.
  3. As the name suggests, it is used to set the password of your database. By default, this is blank in most databases, and you can also set your own database password.
  4. It is used to specify the name of the database that you have created in phpMyAdmin, MySQL, etc.
  5. In this dbdriver, you can specify the type of databases such as MySQL, Postgre SQL, ODBC, MySQLi, and MS SQL.

It also contains some database driver such as PDO, Oracle, ODBC and PostgreSQL that require full DSN string to be connected with database.

For example:

Failover database connection

You can also set a failover database connection when the main connection is not working. This can be achieved by setting the connection as shown below:

Multiple Database Configuration

A Codeigniter also provides a suitability to add multiple database connection with a default connection that can work simultaneously in the database.php file, as follows.

//Default database configuration

//Another database configuration

// third database configuration

After connecting the multiple database connection, you must to load a database in the CodeIgniter application as follows:

The first parameter contains the ‘database name’, whereas second parameter contains ‘TRUE’ to get the database object.


How to use multiple database in CodeIgniter?

Now, if you want to access the multiple connection by the database object, follows the given below syntax:

How to call a database connection in the CodeIgniter application?

There are two ways to call a database connection in the application as:

It is an automatic connection that automatically loads the database library into the Codeigniter application, and this connection can be made using the application/config/autoload.php file.


In manual connection, it allows you to load database connection on some page of the class controller or to access it globally for that particular class.


What is $active_group?

A $active_group tells the system to access a globally defined database that is located in the config/database.php file. And if you want to add more active group, you can specify the database in $active_group, as shown below:

What is a Query Builder?

A Query Builder class that globally enables you to perform a query to the database by setting the $query_builder variables to TRUE/FALSE in the database configuration file. The default setting of the query builder is TRUE, and if you are not using the query builder, set it to FALSE.

Reconnecting or Keeping the connection is alive

In some situations, where the database server’s idle timeout exceeded due to heavy PHP processing in the CodeIgniter application. Therefore, you should reconnect the server by using the reconnect() method before performing further queries to your database. That way, you can keep the connection alive or reconnect it.

Closing the Connection

If you want to close a database connection, you must manually close the connection by executing the following syntax-

Pin It on Pinterest

Share This