This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

📣 Try the fastest hosting platform with pay-as-you-go pricing & 24/7 expert support! MIGRATE NOW →

How To Create Multiple Database Connections in CodeIgniter Applications

Updated on March 16, 2022

2 Min Read
codeigniter multiple database

In real world CodeIgniter projects, developers need to work with multiple databases at the same time. This presents a unique challenge to developers. Since this is a common enough problem, CodeIgniter offers a simple solution for it.

You might also like: How To Host CodeIgniter PHP On Cloud Using SSH

In order to use multiple database connections in your CodeIgniter project, you just need to create multiple configuration arrays that simplify working with multiple databases.

Multiple Database Connections in CodeIgniter

If your application is built with the CodeIgniter framework, it’s exceptionally simple to use multiple databases. CodeIgniter provides an easy way to put through and utilize numerous databases on the same or distinct server. You simply require a few negligible steps to get through to more than one database in the CodeIgniter application.

Moreover, Codeigniter gives numerous database associations in a single app. You simply need to add a database setup cluster to the database “.php record.” Now, you can stack particular database information by using

"$this->load->database('another_db', True);" help

The Default Configuration Array

Following is the structure of the default Codeigniter database configuration array:

$db['default']['hostname'] = 'localhost';

$db['default']['username'] = 'root';

$db['default']['password'] = '';

$db['default']['database'] = 'cloudwaysdb';

$db['default']['dbdriver'] = 'mysql';

$db['default']['dbprefix'] = '';

$db['default']['pconnect'] = TRUE;

$db['default']['db_debug'] = FALSE;

$db['default']['cache_on'] = FALSE;

$db['default']['autoinit'] = FALSE;

$db['default']['stricton'] = FALSE;

$db['default']['cachedir'] = '';

$db['default']['char_set'] = 'utf8';

$db['default']['dbcollat'] = 'utf8_general_ci';

$db['default']['swap_pre'] = '';

So in order to create another database connection, you should create another configuration array. This array has to follow the same structure. Here is an example of the array:

$db['anotherdb']['hostname'] = 'XXX.XXX.X.XXX';

$db['anotherdb']['username'] = 'another_user';

$db['anotherdb']['password'] = '';

$db['anotherdb']['database'] = 'anothercloudwaysdb';

$db['anotherdb']['dbdriver'] = 'mysql';

$db['anotherdb']['dbprefix'] = '';

$db['anotherdb']['pconnect'] = TRUE;

$db['anotherdb']['db_debug'] = FALSE;

$db['anotherdb']['cache_on'] = FALSE;

$db['anotherdb']['cachedir'] = '';

$db['anotherdb']['char_set'] = 'utf8';

$db['anotherdb']['dbcollat'] = 'utf8_general_ci';

$db['anotherdb']['swap_pre'] = '';

$db['anotherdb']['autoinit'] = FALSE;

$db['anotherdb']['stricton'] = FALSE;

Stop Wasting Time on Servers

Cloudways handle server management for you so you can focus on creating great apps and keeping your clients happy.

Connect to the Right Database

At this point, you have two databases in your sample project. To connect to a specific database, you must specify the database name. Here is the proper syntax:

this->load->database(anotherdb, TRUE)

After connecting to the database, you can perform databse operations as shown below:

// load 'anothercloudwaysdb'

$this->legacy_db = $this->load->database(anothercloudwaysdb, true);

// fetch result from 'cloudwaysdb'

$this->legacy_db->select ('*');

$this->legacy_db->from ('cloudwaysdb');

$query = $this->legacy_db->get();

$result = $query->result ();

Now if you need to work with the second database, you have to send the connection to a variable that is usable in your model:

function db_calling_model_method()

{

   $otherdb = $this->load->database('anotherdb', TRUE); // the TRUE paramater tells CI that you'd like to return the database object.

   $query = $otherdb->select('column_one, column_two')->get('table');

   var_dump($query);

}

Close the Connections

CodeIgniter does close the database connections after it determines that the code no longer need the connection. However, as a good practice, developers should close the connections explicitly. Here is how to take care of the issue:

$this->db->close(); // for default Connection

$this->legacy_db->close(); // for second Connection

You might also like: How To Use Elasticsearch Codeigniter Library In Your Projects

Conclusion

In this article, I have discussed the problem and the solution to using multiple database connections in CodeIgniter projects built on any PHP Hosting. If you need help with implementing the idea in your projects, do leave a comment below and I will get back to you.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Inshal Ali

Inshal is a Content Marketer at Cloudways. With background in computer science, skill of content and a whole lot of creativity, he helps business reach the sky and go beyond through content that speaks the language of their customers. Apart from work, you will see him mostly in some online games or on a football field.

×

Get Our Newsletter
Be the first to get the latest updates and tutorials.

Thankyou for Subscribing Us!

×

Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Want to Experience the Cloudways Platform in Its Full Glory?

Take a FREE guided tour of Cloudways and see for yourself how easily you can manage your server & apps on the leading cloud-hosting platform.

Start my tour

CYBER WEEK SAVINGS

  • 0

    Days

  • 0

    Hours

  • 0

    Mints

  • 0

    Sec

GET OFFER

For 4 Months &
40 Free Migrations

For 4 Months &
40 Free Migrations

Upgrade Now