CodeIgniter Database Configuration

In this tutorial you will learn about the CodeIgniter Database Configuration and its application with practical example.

Before, you start interacting with application database and perform some database operation; you must have configured your application to use the database. CodeIgniter framework comes with an advanced implementation of the PHP Active Record Pattern, which makes it very easy to interact with application database and performing CRUD (Create, Read, Update, Delete) operations.

CodeIgniter Database Configuration

In order to establish database connection, we have to make some configuration changes in our database config file which is stored in application/config directory at application/config/database.php. Let’s open database.php file, here you will find following lines of code –

Database configuration settings are stored in an array, change the default values for hostname, username, password, and database with your database details.

Usually, you keep database connection settings in default database group. But, if you want to connect multiple database then you will need to add another database group to $db config array. This database group will keep the connection detail for this specific database connection.

Recommended:-  CodeIgniter Database Caching

Connecting Database In CodeIgniter

Before, you start interacting with application database you must have database connection available. In CodeIgniter, we can connect database in following two ways –

Automatically Connecting:- In this method, we set database library class to be autoloaded. It makes database instance to be available automatically throughout the app. In order to enable auto connect open the autoload configuration file located at application/config/autoload.php, find the $autoload[‘libraries’] array, and modify it to auto-load the database library, as following –

Manually Connecting:- This method is useful when only some of your pages require database connectivity. To obtain the database connectivity add the following line of code in specific function, or in your class constructor to make database connection available globally in that class.

Connecting Multiple Databases In CodeIgniter

Sometimes you may require to use two or multiple database in your application. If you need to connect to more than one database simultaneously. To do this you will need to add another database group to $db config array in database config file which is stored in application/config directory at application/config/database.php.

Recommended:-  CodeIgniter Helloworld Application

Example:-

Once you defined the database group, you can obtain the database instance as following –

Recommended:-  CodeIgniter Method Chaining

Syntax:-

Replace <db-group-name> with the your database group name, In above function first parameter is database group name which you have define in database.php file and setting the second parameter to TRUE return the database object.

Example:-

Let’s Retrieve data from default group database

Let’s Retrieve data from db2 group database

Closing Database Connection In CodeIgniter

CodeIgniter takes care of closing your database connections automatically, but if in case you want to close the database connection explicitly then it can done using close() method as following –

 

In this tutorial we have learn about the CodeIgniter Database Configuration and its application with practical example. I hope you will like this tutorial.