简体   繁体   中英

switch between two databases for different users in codeigniter

I need to change the database name in database.php passing value from the controller. I tried sessions and env variable. but in the database.php cannot access the value from sessions and env variable. also, i need to use the default database to load the page and when user has been logged in need to switch the database. i'm doing this for reducing size of the database data is there any solution really glad someone can help me.

databse.php

$active_group = 'default';
$query_builder = TRUE;


if($db_val==""){
    $db['default'] = array(
        'dsn'   => '',
        'hostname' => 'localhost',
        'username' => 'root',
        'password' => '',
        'database' => 'sliate_srs',
        '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
    );
}
else{

    $db['default'] = array(
        'dsn'   => '',
        'hostname' => 'localhost',
        'username' => 'root',
        'password' => '',
        'database' => getenv("DB_year"),
        '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
    );
}

controller.php function loginSubmit() {

   $DB_year=$this->input->post('year_select'); 
    //$this->session->unset_userdata('DB_year');
  
    

    
    //$this->session->set_userdata('DB_year',$DB_year);
    $DB_year="srs_2019";
    putenv("DB_year=$DB_year");
    // $_ENV["DB_year"]="srs_2019";
    print_r(getenv("DB_year"));
    // $DB_year="srs_2019";
    

   
    $result = $this->Login_model->authenticateLogin(); 
    
    if (!empty($result)) {
        $now = date('Y-m-d H:i:s');
        $ip = $this->input->ip_address();
        $data = array(
            'u_id' => $this->session->userdata('u_id'),
            'u_name' => $this->session->userdata('u_name'),
            'center_name' => $this->session->userdata('u_branch'),
            'last_login_ip' => $this->input->ip_address('ip'),
            'last_login_date_time' => $now
        );
        $name = $this->session->userdata('DB_year');
        print_r($name);
        $this->Login_model->last_login($data);
        redirect('Admin/dashboard');
    } else {
    
        redirect('Login?login=invalid');
    }
    
}

Instead of changing the config group in Database.php, why not create a custom connection in your model, as explained in Connecting with Custom Settings ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM