Change the database name on the fly in CodeIgniter 3 Change the database name on the fly in CodeIgniter 3 codeigniter codeigniter

Change the database name on the fly in CodeIgniter 3


so you can get the connection details from the session and connect to the database manually using those details and close it when done

Manually Connecting to a Database

$this->load->database();

The first parameter of this function can optionally be used to specify a particular database group from your config file, or you can even submit connection values for a database that is not specified in your config file. Examples:

To choose a specific group from your config file you can do this:

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

Where group_name is the name of the connection group from your config file.

To connect manually to a desired database you can pass an array of values:

$config['hostname'] = "localhost";$config['username'] = "myusername";$config['password'] = "mypassword";$config['database'] = "mydatabase";$config['dbdriver'] = "mysql";$config['dbprefix'] = "";$config['pconnect'] = FALSE;$config['db_debug'] = TRUE;$config['cache_on'] = FALSE;$config['cachedir'] = "";$config['char_set'] = "utf8";$config['dbcollat'] = "utf8_general_ci";$this->load->database($config);

For information on each of these values please see the configuration page.

Or you can submit your database values as a Data Source Name. DSNs must have this prototype:

$dsn = 'dbdriver://username:password@hostname/database';$this->load->database($dsn);

To override default config values when connecting with a DSN string, add the config variables as a query string.

$dsn = 'dbdriver://username:password@hostname/database?char_set=utf8&dbcollat=utf8_general_ci&cache_on=true&cachedir=/path/to/cache';$this->load->database($dsn);

Manually closing the Connection

While CodeIgniter intelligently takes care of closing your database connections, you can explicitly close the connection.

$this->db->close();

for more about connecting to multiple databases read this https://ellislab.com/codeigniter/user-guide/database/connecting.html


I solved the issue this way which seems clean to me unless someone sees a security issue.

set session in auto load.

In database.php

 $ci = get_instance(); if (!isset($ci->session->userdata['clientdb'])){      $ci->session->set_userdata('clientdb','database1'); } $active_group = 'default'; $query_builder = TRUE; $db['default'] = array(      ........      'database' => $ci->session->userdata['clientdb'],      .......... );

The database value is set via the cleintdb value of the users session.


here is your solution. build all links with corresponding client ID and change database.php like

if (isset($_REQUEST['clid'])) {    $efg = (int) $_REQUEST['clid'];} else {  die('wrong URL');}$dbh = new PDO('mysql:host=localhost;dbname=client_db,client_dbuser,clientdb_pass'); $sql = 'SELECT db_username,db_name,db_pass FROM clients WHERE id=?';$sth = $dbh->prepare($sql); $sth->execute(array($efg));$d_result = $sth->fetchAll(PDO::FETCH_ASSOC);if (count($d_result) < 1) {  die('Wrong client');}$active_group = 'default';$query_builder = TRUE;$db['default'] = array('dsn' => '','hostname' => 'localhost','username' => $d_result[0]['db_username'],'password' => $d_result[0]['db_pass'],'database' => $d_result[0]['db_name'],'dbdriver' => 'mysqli','dbprefix' => '','pconnect' => TRUE,'db_debug' => TRUE,'cache_on' => FALSE,'cachedir' => '','char_set' => 'utf8','dbcollat' => 'utf8_unicode_ci','swap_pre' => '','encrypt' => FALSE,'compress' => FALSE,'stricton' => FALSE,'failover' => array(),'save_queries' => TRUE);

I do collect $d_result[0]['db_username'], $d_result[0]['db_pass'] etc from client database. You may take it from session or whatever. hope it helps.