How to change default database in codeigniter?

by raven_corwin , in category: PHP Frameworks , 2 months ago

How to change default database in codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by elise_daugherty , 2 months ago

@raven_corwin 

To change the default database in CodeIgniter, you need to modify the database configuration file located at application/config/database.php.

  1. Open the database.php file in a text editor.
  2. Locate the default array in the configuration file. This array contains the settings for the default database connection.
  3. Modify the settings in the default array to specify the new database connection details. You will need to provide the host, username, password, and database name for the new database.
  4. Save the changes to the database.php file.
  5. You can now use the new database connection in your CodeIgniter application by simply referring to the default group in the database configuration.


For example, if your default group is named 'default', you can load the database in your models like this:

1
$this->load->database(); 


Alternatively, you can also specify the database connection group in your models, controllers, or other components like this:

1
$this->load->database('group_name');


By following these steps, you can easily change the default database in your CodeIgniter application.