简体   繁体   English

如何处理基于用户的数据库选择?

[英]How to handle selecting a database based on the user?

I am building a website that will have multiple users from multiple companies. 我正在建立一个网站,它将有来自多家公司的多个用户。 Each company is given a copy of the same database with the same tables. 每个公司都将获得具有相同表的相同数据库的副本。 The only difference is the database name. 唯一的区别是数据库名称。 I would like for each user to only have access to the database assigned to their company. 我希望每个用户只能访问分配给他们公司的数据库。

I need to come up with a way to select the database based on the user's login. 我需要提出一种基于用户登录名选择数据库的方法。 Is it acceptable to use a session var from a users table as the database name?... 使用用户表中的会话变量作为数据库名称是否可以接受?...

$dbCon = new mysqli('localhost', 'root', '', $_SESSION['databaseName']);

Maybe a cookie? 也许是饼干? Or am I looking at the whole thing wrong. 还是我看错了整个事情。 Any input would be great. 任何输入都会很棒。 I'm still learning. 我还在学习。 Thanks 谢谢

My suggestion is to pick one database as your "login" database that has all the user accounts in a table. 我的建议是选择一个数据库作为您的“登录”数据库,该数据库具有表中的所有用户帐户。 Then add a column to the user table that you are authenticating their login against. 然后,在要验证其登录名的用户表中添加一列。 You can store the company or "assigned database name" in that column. 您可以在该列中存储公司或“分配的数据库名称”。 Once authenticated you can close your DB connection and open up a new connection with the correct assigned database. 身份验证后,您可以关闭数据库连接,并使用正确的已分配数据库打开新连接。

You can have following schema for you user login 您可以为用户登录使用以下架构

LoginTable : (username,password,...,company_id) LoginTable :( (username,password,...,company_id)

Company : (company_id,....,dbname) 公司(company_id,....,dbname)

So as soon as user login he can select the database corresponding the company id from the company table. 因此,一旦用户登录,他就可以从公司表中选择与公司ID相对应的数据库。 The query can be written in a single command by natural joining company and LoginTable.Hence after retrieving the dbname you can store it in session variable for further use. 该查询可以由自然加入公司和LoginTable用一个命令编写。因此,检索dbname之后,可以将其存储在会话变量中以备将来使用。

I hope you understood. 希望你能理解。

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

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