In this CodeIgniter database connection we are going to learn how we can simply create connectivity between application and database through CodeIgniter. The database has core importance in any application to store the relational data. Codeigniter provide as rich functions to interact with the database in a simple way.

Basically, there are two methodologies which are followed by professionals to create CodeIgniter database connection within the application.

Automatically load database

This is a simple future forward by the CodeIgniter Framework in which we can declare the database instance to be automatically loaded in the application when its get executed. There are some pros and cons of using this automatic loading feature. For depth read CodeIgniter Database Configuration and CodeIgniter Autoload Configuration.

Pros

  • We don’t need to load the database in the application again and again.
  • Line of code get optimized.
  • Partially safe time in coding.
  • Database loaded for the whole application score.

Cons

  • It increases the load of the application.
  • Database is loaded every time and consume memory.
  • The efficiency of the application get reduced.

Manually load database

Mostly developer uses this approach to load the database within the application. Because it provides Limited scope and increases the efficiency of application regarding the data exchange and page speed. Whenever you need to interact with the database you can simply be loaded within that scope to interact with the database. Like in the following code we are loading the database.

/* == It will load the default instance of database which is declared in the database.php == */
$this->load->database();

Pros

  • Increase page speed and efficiency of application.
  • It will not consume memory every time.

Cons

  • Line of code will be increased
  • We have to load database again and again
  • More time is consumed to write code full loading database.

Remember that we don’t need to pass any kind of parameter within the database instance method because we have already defined database configuration in our previous tutorial. It will automatically load the default instance of the database along with its configuration.