How to connect with user specified database in codeigniter

codeigniter, connection, database, dynamic, php

Solution

According to the guide, you can manually pass database connectivity settings via the third parameter of `$this->load->model`:

$config['hostname'] = "localhost";
$config['username'] = "myusername";
$config['password'] = "mypassword";
$config['database'] = "mydatabase";
$config['dbdriver'] = "mysql";
$config['dbprefix'] = "";
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;

$this->load->model('Model_name', '', $config);
// or as gorelative notes, to access multiple databases:
$DB2 = $this->load->database($config, TRUE);

Problem

I have a project in which i have to connect with user specified database. I want to implement it in a proper codeigniter's style but i dont know how can i do that codeigniter stores database credentials in a database.php file is there any way to make it dynamic. Or is there any other approach for achieving this? I have googled it but did not find anything helpful. Any help and suggestion would be appreciated. UPDATE: The project is about reporting. I have a form in which i got the database login credentials and then generate the report about their database everything would be done on runtime.

Original source