LoginSignup
0
0

More than 5 years have passed since last update.

CodeIgniter: Create Connect Multiple Databases

Last updated at Posted at 2017-11-02

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.

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;

Read full article at: https://www.cloudways.com/blog/connect-multiple-databases-codeigniter/

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0