在 CodeIgniter 3 中即时更改数据库名称

Change the database name on the fly in CodeIgniter 3

我需要在验证用户后更改数据库名称。在 database.php 文件中有这个。

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

我知道你可以用$this->db->database显示数据库的值。我想在用户会话期间将其更改为另一个名称。

我已经尝试使用 $this->db->set_database('databasename2') 和其他各种尝试,但没有解决方案。我也搜索过,没找到解决方案。

有人知道如何在用户会话期间更改数据库名称吗?

这是您的解决方案。使用相应的客户端 ID 构建所有链接,并将 database.php 更改为

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

我确实从客户端数据库收集 $d_result[0]['db_username'], $d_result[0]['db_pass'] 等。您可以从会话或其他任何地方获取它。希望对你有帮助。

因此您可以从会话中获取连接详细信息并使用这些详细信息手动连接到数据库并在完成后关闭它

手动连接到数据库

$this->load->database();

此函数的第一个参数可以选择用于指定配置文件中的特定数据库组,或者您甚至可以提交配置文件中未指定的数据库的连接值。示例:

要从您的配置文件中选择一个特定的组,您可以这样做:

$this->load->database('group_name');

其中 group_name 是您的配置文件中的连接组的名称。

要手动连接到所需的数据库,您可以传递一组值:

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

有关每个值的信息,请参阅配置页面。

或者您可以将数据库值作为数据源名称提交。 DSN 必须具有此原型:

$dsn = 'dbdriver://username:password@hostname/database';

$this->load->database($dsn);

要在使用 DSN 字符串连接时覆盖默认配置值,请将配置变量添加为查询字符串。

$dsn = 'dbdriver://username:password@hostname/database?char_set=utf8&dbcollat=utf8_general_ci&cache_on=true&cachedir=/path/to/cache';

$this->load->database($dsn);

手动关闭连接

虽然 CodeIgniter 会智能地关闭您的数据库连接,但您可以显式关闭连接。

$this->db->close();

有关连接到多个数据库的更多信息,请阅读此 https://ellislab.com/codeigniter/user-guide/database/connecting.html

我用这种方式解决了这个问题,除非有人发现安全问题,否则我觉得很干净。

在自动加载中设置会话。

在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'],
      ..........
 );

数据库值是通过用户会话的 cleintdb 值设置的。