Join two connections in a PHP MYSQLI

0

I have mounted a web page that accesses some data in concrete with data base.table database.table (this way the queries are mounted)

the problem that I have now been told that database1 is on another server and I do not know how to create two connections and be able to make queries by joining the two connections, since to launch a query is:

$consulta= $mysqli->query($query); ( $mysqli is the connection.)

    
asked by WindStone 06.03.2017 в 10:25
source

3 answers

1

since they are two different connections (it is unimportant if they are the same server or different), you must handle the queries separately. Use different connectors and run your queries on one or the other according to where the data is. Finally, make the union of data by code, if it is necessary to make the union of data.

    
answered by 06.03.2017 / 10:38
source
0

I do not know if I understand your question correctly.

If $ mysqli is the connection, with one of the two DBs (obviously it is with which you have the connection data), to connect to the other, you can re-instantiate $ mysqli , with the connection data of the other BDs (at least the IP will change), or create a different connection instance with the same data, if you need to work simultaneously with both BDs.

With two instances of type connection is more flexible and you are using them or not, it becomes more understandable, so you would have for example $ mysqli1 and $ mysqli2 . Now you just have to know where the data you require at any given time is to make the queries with the appropriate instance.

    
answered by 06.03.2017 в 10:43
-1
  

the problem that I have now been told that database1 is on another server

I would ask myself three questions to find the best solution:

(A). If it is not mandatory that the two BDs are separated

If it is not, it would be best to have the two BDs on the same server, because you avoid that your site depends on another domain . Suppose the domain where the other BD is is down, does not work for a few hours or a day, or is hacked ... The parts of your domain that show data from that "external" DB would not work, maybe many pages of your web be shown blank.

If there is no other output and the DBs must be on different servers, then there are several possibilities to call the data from your "external" DB. The safest thing, I think, would be to consult said "external" DB in the form of an API. You would consult URLs of the same that would return the values you need in JSON format and present these formats on your page. But ... for that you must have access to the other domain and program your API to return the data you need.

(B). If the two BDs can be in the same domain

If this is possible, excellent! It would be a better solution than (A) . Well, you can create two connections to each BD, as explained in previous responses and comments. Ask each BD for the data you need and show them on your page. So you stop depending on whether the other domain works or not.

But ... I would still ask another question.

(C). If it is mandatory that the two BDs are separated

If it is not, the best thing of all would be to have a unique BD with all the tables of BD1 and BD2.

You would gain speed and efficiency on your website. Having a supplementary connection always has its price, as well as consulting two BDs instead of one. The difference is also noticeable when programming.

(D). In summary

Everything will depend on what you can or can not do in your case and how you want to use it. But, within the possibilities, choose what is most practical and most effective.

    
answered by 06.03.2017 в 13:34