Search code examples
phpmysqlcodeignitercodeigniter-3

Change dynamically a database in Codeigniter


I want to change databse after login as I am using session for I cannot get database correctly...

And Need to use that database as default in codeigniter......

The Database name, Hostname, Password and Username are commoing from the another database... after login from the databse1 I need to all other stuff from another database dynamically

This is my controller for login:

$unme = $this->db->get_where('database_manage_user_list', array('hospital_id' => $iidd_fnl))->row()->username;
$pss = $this->db->get_where('database_manage_user_list', array('hospital_id' => $iidd_fnl))->row()->password;
$hstnmw = $this->db->get_where('database_manage_user_list', array('hospital_id' => $iidd_fnl))->row()->hostname;
$dbnme = $this->db->get_where('database_manage_user_list', array('hospital_id' => $iidd_fnl))->row()->databasename;

$_SESSION["username"] = $unme; 
$_SESSION["password"] = $pss; 
$_SESSION["hostname"] = $hstnmw; 
$_SESSION["databse"] = $dbnme; 
if($grpid == '11' and $by != 'yes'){
  redirect('home');
}

And my code in database.php is:

$unme = "";
$pass = "";
$host = "";
$data = "";

if(!isset($_SESSION['username'])){
    $unme = "username1";
}else{
    $unme = $_SESSION['username'];
}
if(!isset($_SESSION['password'])){
    $pass = "password1";
}else{
    $pass = $_SESSION['password'];
}
if(!isset($_SESSION['hostname'])){
    $host = "localhost";
}else{
    $host = $_SESSION['hostname'];
}
if(!isset($_SESSION['databse'])){
    $data = "database1";
}else{
    $data = $_SESSION['datbase'];
}

$db['default'] = array(
    'dsn'   => '',
    'hostname' => $host,    
    'username' => $unme,
    'password' => $pass,
    'database' => $data,
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

But after loging in it is not using database2 it is always using databse1

I don't know why?

Is there any mistake in my code?


Solution

  • You can add a second database in the `application/config/database.php´ file.

    Normally, you would set the default database group, like so:

    $db['default']['hostname'] = "localhost";
    $db['default']['username'] = "root";
    $db['default']['password'] = "";
    $db['default']['database'] = "database_name";
    $db['default']['dbdriver'] = "mysql";
    $db['default']['dbprefix'] = "";
    $db['default']['pconnect'] = TRUE;
    $db['default']['db_debug'] = FALSE;
    $db['default']['cache_on'] = FALSE;
    $db['default']['cachedir'] = "";
    $db['default']['char_set'] = "utf8";
    $db['default']['dbcollat'] = "utf8_general_ci";
    $db['default']['swap_pre'] = "";
    $db['default']['autoinit'] = TRUE;
    $db['default']['stricton'] = FALSE;
    

    Notice that the login information and settings are provided in the array named $db['default'].

    You can then add another database in a new array - let's call it 'otherdb'.

    $db['otherdb']['hostname'] = "localhost";
    $db['otherdb']['username'] = "root";
    $db['otherdb']['password'] = "";
    $db['otherdb']['database'] = "other_database_name";
    $db['otherdb']['dbdriver'] = "mysql";
    $db['otherdb']['dbprefix'] = "";
    $db['otherdb']['pconnect'] = TRUE;
    $db['otherdb']['db_debug'] = FALSE;
    $db['otherdb']['cache_on'] = FALSE;
    $db['otherdb']['cachedir'] = "";
    $db['otherdb']['char_set'] = "utf8";
    $db['otherdb']['dbcollat'] = "utf8_general_ci";
    $db['otherdb']['swap_pre'] = "";
    $db['otherdb']['autoinit'] = TRUE;
    $db['otherdb']['stricton'] = FALSE;
    

    Now, to actually use the second database, you have to send the connection to another variable that you can use in your model:

    function my_model_method()
    {
      $otherdb = $this->load->database('otherdb', TRUE); // the TRUE parameter tells CI that you'd like to return the database object.
    
      $query = $otherdb->select('first_name, last_name')->get('person');
      var_dump($query);
    }
    

    That should do it. The documentation for connecting to multiple databases can be found here: http://codeigniter.com/user_guide/database/connecting.html

    Or you can switch between databases through the control statement. Like -

     if(your_condition){
        
            $db_name  = 'db_name';
            $u_name   = 'u_name';
            $hostname = 'hostname';
            $password = 'password';
        }
        
        $db['default'] = array(
            'dsn'  => '',
            'hostname' => $hostname,
            'username' => $hostname,
            'password' => $password,
            'database' => $db_name,
            'dbdriver' => 'mysqli',
            'dbprefix' => '',
            'pconnect' => FALSE,
            'db_debug' => (ENVIRONMENT !== 'production'),
            'cache_on' => FALSE,
            'cachedir' => '',
            'char_set' => 'utf8',
            'dbcollat' => 'utf8_general_ci',
            'swap_pre' => '',
            'encrypt' => FALSE,
            'compress' => FALSE,
            'stricton' => FALSE,
            'failover' => array(),
            'save_queries' => TRUE
        );
    

    Thanks.