简体   繁体   English

使用 PHP MySQL mysqli 从两个数据库中获取数据

[英]Fetch data from two databases using PHP MySQL mysqli

I have written a query in PhpMyadmin to fetch data from two databases running in the same server, and is working fine.我在 PhpMyadmin 中编写了一个查询,以从同一服务器上运行的两个数据库中获取数据,并且工作正常。 But I am not sure how to use this query in PHP code.但我不确定如何在 PHP 代码中使用此查询。 This query contains RIGHT JOIN and other sub-queries.此查询包含 RIGHT JOIN 和其他子查询。 Even if I open two DB connections, how will I execute this query and which Connection should I use?即使我打开两个数据库连接,我将如何执行此查询以及我应该使用哪个连接?

Eg.例如。

Database A: Table: accountmaster (contains the profile details of the users, with IDs for City, State etc) Database B: All masters (City, State etc)数据库 A:表:accountmaster(包含用户的配置文件详细信息,ID 为 City、State 等) 数据库 B:所有主服务器(City、State 等)

Query: fetching all profile entries by joining these two databases.查询:通过加入这两个数据库来获取所有配置文件条目。

If you share your query, I think we could elaborate on more precise answers.如果您分享您的查询,我认为我们可以详细说明更准确的答案。

Probably your question is already answered here .可能您的问题已经在这里得到解答。

Firstly, you should use one connection from any database.首先,您应该使用来自任何数据库的一个连接。 I would prefer to connect to database A, once it contains the references to database B and makes sense for me to request join data from B.我更愿意连接到数据库 A,一旦它包含对数据库 B 的引用并且对我来说从 B 请求连接数据是有意义的。

As discussed in the link, it is necessary that the user has the privileges to execute the query.如链接中所述,用户必须具有执行查询的权限。

Finally, it is just to write your query as discussed here - as a string - and pass it as a parameter to mysqli query .最后,只需按照此处讨论的方式编写查询 - 作为字符串 - 并将其作为参数传递给mysqli query

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

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