在Codeigniter中动态更改数据库

xqnpmsa8  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(199)

我想在登录后更改数据库,因为我正在使用会话,因为我无法正确获取数据库...
需要使用该数据库作为默认的codeigniter......
数据库名称、主机名、密码和用户名来自另一个数据库...从数据库登录后,我需要动态地从另一个数据库获取所有其他内容
这是我的登录控制器:

$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');
}

我在database.php中的代码是:

$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
);

但登录后,它并不使用database2,而是始终使用database1
不知道为什么?
我的代码有什么错误吗?

myss37ts

myss37ts1#

您可以在'application/config/database.php'文件中添加第二个数据库。
通常,您会设定预设数据库群组,如下所示:

$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;

请注意,登录信息和设置在名为$db ['default']的数组中提供。
然后,您可以在新数组中添加另一个数据库-我们将其命名为“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;

现在,要实际使用第二个数据库,必须将连接发送到可以在模型中使用得另一个变量:

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);
}

可以在这里找到连接到多个数据库的文档:http://codeigniter.com/user_guide/database/connecting.html
或者,您可以通过控制语句在数据库之间切换。

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
    );
  • 谢谢-谢谢

相关问题