简体   繁体   中英

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. But I am not sure how to use this query in PHP code. This query contains RIGHT JOIN and other sub-queries. 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)

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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