一张表有不同的表前缀?

发布于 2024-12-15 22:28:00 字数 398 浏览 2 评论 0原文

假设,假设我多次安装了一些奇怪的 MySQL/PHP 驱动软件。它们是相同的软件,因此所有安装的数据库表结构都是相同的。我想做的是让软件仅对一个表使用不同的数据库前缀。具体来说,是一个用户表。因此,假设安装设置如下:

  • 主要安装:/home/www/main,数据库主,前缀为1
  • 第二次安装:/home/www/second,数据库主,前缀为2
  • 第三次安装:/home/www/third,数据库主,前缀为3

所以我想要的是告诉第二个安装和第三个安装通过该表从前缀为1的用户表中提取自己的数据。因此,在主安装上注册的任何用户也会在第二个和第三个安装上注册,反之亦然。有可能吗?如果可能的话我会怎么做?即使这只是一个提供相同基本结果的解决方法,我也会很高兴。

Hypothetically, let's say I had multiple installations of some odd MySQL/PHP driven software. They are the same software so the database table structure is the same cross all of the installs. What I want to do, is make the software use a different database prefix for just one table. Specifically, a user table. So say the installs are set up like this:

  • Main install: /home/www/main, database main, prefix is1
  • Second install: /home/www/second, database main, prefix is2
  • Third install: /home/www/third, database main, prefix is3

So what I want is to tell second install and third install to pull from the users table on prefix is1 for its own data via that table. Thus, any user that registers on main install is also registered on second and third and vice versa. Is it possible, and how would I do it if it is? Even if it's just a workaround that gives the same basic result I would be happy.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暖树树初阳… 2024-12-22 22:28:00

如果您不想修改应用程序的 PHP 源代码,并且在这方面尚未可配置,那么另一个选择是修改数据库,更改 is2usersis3users 成为 is1users 上的视图:(

DROP TABLE is2users;
CREATE VIEW is2users AS SELECT * FROM is1users;
DROP TABLE is3users;
CREATE VIEW is3users AS SELECT * FROM is1users;

参见 http://dev.mysql.com/doc/refman/5.0/en/views.html 有关视图的一般信息,http://dev.mysql.com/doc/refman/5.0/en/create-view.html 为具体有关 CREATE VIEW 的信息。)

取决于应用程序,这可能无法完美工作 - 例如,应用程序可能会在内存中缓存一些信息(例如标识符序列的当前值) - 但它可能可以工作。在投入生产之前进行测试!

If you don't want to modify the app's PHP source-code, and it's not already configurable in this respect, then another option is to modify the database, changing is2users and is3users to be views on is1users:

DROP TABLE is2users;
CREATE VIEW is2users AS SELECT * FROM is1users;
DROP TABLE is3users;
CREATE VIEW is3users AS SELECT * FROM is1users;

(See http://dev.mysql.com/doc/refman/5.0/en/views.html for general information on views, http://dev.mysql.com/doc/refman/5.0/en/create-view.html for information on CREATE VIEW specifically.)

Depending on the app, this may not work perfectly -- for example, the app might cache some information in memory (such as the current value of an identifier-sequence) -- but it will probably work. Test it before putting it in production!

留一抹残留的笑 2024-12-22 22:28:00

内容中是这样的

$prefix = 'is3'

您的 php 代码可能在 cfg.php:和 user.model.php: 之类的

$sql = 'SELECT * FROM `'.$prefix.'users`';

,因此您需要更改三个安装代码中的两个以使用“用户”表。但这似乎太危险了。

Your php code likely goes something like this in something like cfg.php:

$prefix = 'is3'

and in something like user.model.php:

$sql = 'SELECT * FROM `'.$prefix.'users`';

So you need to change in two of three installs code for working with 'users' table. But it seems to be too dangerous.

π浅易 2024-12-22 22:28:00

这个设置很简单,模式方面。您提到“安装”,这意味着您正在使用一些打包的库,其中可能包含一个配置文件,您可以在其中更改各种设置,并且其中一个设置很可能是表前缀。如果没有表前缀选项,您可以浏览安装代码并查找架构所在的位置,并为每次安装手动更改前缀。

现在是最困难的部分,让代码按照您所描述的方式运行。您必须使您的应用程序了解所有三个数据库,这意味着您可能必须向代码添加两个新的数据库连接器(每个数据库一个连接器)。然后,您必须添加逻辑来处理用户 CRUD 功能,以便在所有三个数据库中插入/更新/删除用户数据(这里的事务会很好)。

您所描述的内容是可能的,但需要一些自定义代码。

The setup of this is easy, schema-wise. You mention 'installs' which means you are using some packaged library which probably contains a config file where you can change various settings, and chances are one of the settings is the table prefix. If there is no table prefix option you can browse the install code and find where the schema is located and change the prefix manually for each install.

Now comes the hardpart, getting the code to behave as you described. You will have to make your app aware of all three databases, meaning you will probably have to add two new database connectors to the code (one connector for each database). Then you will have to add logic to handle user CRUD functionality to insert/update/delete user data across all three databases (transaction would be good here).

What you are describing is possible, but will require some custom code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文