简体   繁体   English

我们能否在运行时根据codeigniter中的用户登录连接到数据库?

[英]Can we connect to database at runtime depending on user login in codeigniter?

I am working on project,where i want to connect to database at run time depending on user login.As i am starting my project now,i wanted to try out code-igniter framework.But after doing research ,i am finding it hard to say that this is possible.Else i have code for doing same in standard php. 我正在研究项目,我想根据用户登录名在运行时连接到数据库。当我现在开始我的项目时,我想尝试代码点火器框架。但是经过研究,我发现很难说这是可能的。否则我有在标准php中执行相同操作的代码。 But i wanted to try framework before going for that approach. 但是我想在尝试这种方法之前先尝试一下框架。 i have code for multiple ( 2 database ) connection. 我有多个(2个数据库)连接的代码。

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'test1';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$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;

//Another database connection.

$db['db2']['hostname'] = 'localhost';
$db['db2']['username'] = 'root';
$db['db2']['password'] = '';
$db['db2']['database'] = 'test2';
$db['db2']['dbdriver'] = 'mysql';
$db['db2']['dbprefix'] = '';
$db['db2']['pconnect'] = FALSE;
$db['db2']['db_debug'] = TRUE;
$db['db2']['cache_on'] = FALSE;
$db['db2']['cachedir'] = '';
$db['db2']['char_set'] = 'utf8';
$db['db2']['dbcollat'] = 'utf8_general_ci';
$db['db2']['swap_pre'] = '';
$db['db2']['autoinit'] = TRUE;
$db['db2']['stricton'] = FALSE;

from the doc 从文档
To connect manually to a desired database you can pass an array of values: 要手动连接到所需的数据库,您可以传递一个值数组:

$config['hostname'] = 'localhost';
$config['username'] = 'myusername';
$config['password'] = 'mypassword';
$config['database'] = 'mydatabase';
$config['dbdriver'] = 'mysqli';
$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);

Select the database in run time in codeigniter using the following statement. 使用以下语句在run时在codeigniter选择数据库。

$this->db->db_select('database2_name');

If you want to close the current connection then use 如果要关闭当前连接,请使用

$this->db->close();//closes the current db connection

Then select the database you have required. 然后select所需的database

https://www.codeigniter.com/user_guide/database/connecting.html https://www.codeigniter.com/user_guide/database/connecting.html

Check out the section "Manually Connecting to a Database" where it says 查看“手动连接到数据库”部分,其中显示

To choose a specific group from your config file you can do this: 要从配置文件中选择特定的组,可以执行以下操作:

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

So after logging in you can call your database connection manually and do queries. 因此,登录后,您可以手动调用数据库连接并进行查询。 I hope this is sufficient. 我希望这足够了。

The $active_group variable lets you choose which connection group to make active. $ active_group变量使您可以选择激活哪个连接组。 By default there is only one group (the 'default' group). 默认情况下,只有一个组(“默认”组)。

$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'test1';
$db['default']['dbdriver'] = 'mysql'; 
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$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;

The $active_record variables lets you determine whether or not to load the active record class. $ active_record变量使您可以确定是否加载活动记录类。

Yes you can connect database at runtime in codeigniter. 是的,您可以在运行时在codeigniter中连接数据库。 By default if you donot specify the name of the database it will take the default database 默认情况下,如果不指定数据库名称,它将采用默认数据库

$db['default']['hostname'] = 'localhost';

and if you specify a particular name 如果您指定一个特定的名称

$db['db2']['hostname'] = 'localhost';

Like this $this->load->database('db2', TRUE); 像这样$this->load->database('db2', TRUE);

I have done same in one of my project , You can create a Table, (if you want dynamic) in which you can store all the database name according to the user login. 我在我的项目之一中做了同样的操作,可以创建一个Table(如果要动态),可以根据用户登录名在其中存储所有数据库名称。

So when connecting to the Database in Model , firstly you need to get the database name from the table you create using user login based on that you can do something like this 因此,在连接到Model中的数据库时,首先需要从使用用户登录名创建的表中获取数据库名称,您可以执行以下操作

Here $dbname is the your Database name that we you found by query to default database . $ dbname是您通过查询默认数据库发现的数据库名称。

 $admin_db= $this->load->database($dbname, TRUE);
 $q = $admin_db->get('Tablename');

@Pratima as per your comment to Yahia Bat you want to get the details of the database dynamically from some table and then connect to the database. @Pratima根据您对Yahia Bat的评论,您希望从某个表动态获取数据库的详细信息,然后连接到数据库。 For that you should populate the $config or any other array with the required details from the database and connect accordingly. 为此,您应该使用数据库中所需的详细信息填充$ config或任何其他阵列,并进行相应连接。

$config['hostname'] = '<<value from database table>>';
$config['username'] = '<<value from database table>>';
$config['password'] = '<<value from database table>>';
$config['database'] = '<<value from database table>>';
$config['dbdriver'] = 'mysqli';
$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);

that should solve the issue 那应该解决问题

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

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