3

I have need to change the database name after a user is verified. In the database.php file there is this.

 $db['default'] = array(....
     'database' => 'databasename1',
...
  );

I know that you can display the value of database with $this->db->database. I would like to change it to another name for the duration of the users session.

I have tried using $this->db->set_database('databasename2') and various other attempts with no solution. I have also searched and can not find the solution.

Anyone know how to change the database name for the duration of the users session?

dutchlab
  • 541
  • 1
  • 13
  • 27
  • why do you need to do that ? – Nassim Jun 17 '15 at 23:20
  • Each company has it's own database and the user log in provides the database name. – dutchlab Jun 17 '15 at 23:31
  • then i guess it's better to put the database name in the session and then call it or rename it from the session , it makes more sense – Nassim Jun 17 '15 at 23:36
  • or you can use multiple databases like in this post http://stackoverflow.com/questions/15348423/codeigniter-multiple-databases-accessing-database-config-in-a-second-database – Nassim Jun 18 '15 at 00:28
  • That is what I am trying to figure out how to do, is get the database name out of the session into the database name in the database.php file when it is read. there is an error that happens when trying to access the $this that states you can not do that when 'not in object context'. – dutchlab Jun 18 '15 at 00:54

3 Answers3

4

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

Nassim
  • 2,879
  • 2
  • 37
  • 39
  • Not working Message: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead Filename: mysql/mysql_driver.php Line Number: 136 Backtrace: File: /var/www/REPO/cgen/application/controllers/Cgen.php Line: 28 Function: database File: /var/www/REPO/cgen/index.php Line: 315 Function: require_once – Kiran Reddy Feb 15 '18 at 11:44
1

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.

dutchlab
  • 541
  • 1
  • 13
  • 27
0

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.

Rejoanul Alam
  • 5,435
  • 3
  • 39
  • 68