在CodeIgniter 3中即时更改数据库名称 [英] Change the database name on the fly in CodeIgniter 3
问题描述
我需要在验证用户后更改数据库名称。
I have need to change the database name after a user is verified. In the database.php file there is this.
$db['default'] = array(....
'database' => 'databasename1',
...
);
我知道您可以使用$ this-> db-> database显示数据库的值。我想在用户会话期间将其更改为另一个名称。
I know that you can display the value of database with $this->db->database. I would like to change it to another name for the duration of the users session.
我尝试使用$ this-> db-> set_database('databasename2')和没有解决方案的其他各种尝试。我也进行了搜索,但找不到解决方法。
I have tried using $this->db->set_database('databasename2') and various other attempts with no solution. I have also searched and can not find the solution.
有人知道如何在用户会话期间更改数据库名称吗?
Anyone know how to change the database name for the duration of the users session?
推荐答案
我以这种方式解决了这个问题,除非有人看到安全问题,否则这对我来说似乎很干净。
I solved the issue this way which seems clean to me unless someone sees a security issue.
设置会话为自动加载。
在database.php中
In 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值设置的。
The database value is set via the cleintdb value of the users session.
这篇关于在CodeIgniter 3中即时更改数据库名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!