简体   繁体   English

codeigniter中不同用户的两个数据库之间切换

[英]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.我需要更改 database.php 中的数据库名称,从 controller 传递值。我尝试了会话和环境变量。 but in the database.php cannot access the value from sessions and env variable.但在数据库中。php 无法访问会话和环境变量中的值。 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数据库.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() { controller.php function 登录提交(){

   $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 ?与其更改 Database.php 中的配置组,不如在 model 中创建自定义连接,如使用自定义设置连接中所述?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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