Connecting and querying two Mysql databases simultaneously with PHP - php

I'm intending to develope a web that interacts with three different Mysql databases in the same server. User and password are the same in all three databases.
I want to use mysqli in procedural mode.
Some times I want to query just one databse, some times two of them, whatever.
What would it be the best way to connect with two of them and then, say, join two tables belonging to different databases?
Thanks in advanced!

If the databases all reside in the same server, you can query any table in any database at any time that you have access to by prefixing it with the database name:
SELECT *
FROM database1.table1
JOIN database2.table2 ON ...
If the databases are on two different servers or need different access rights, there's no way to do this.

Related

How to join two tables in mysql that has different connections of mysql on different server laravel

I tried this query below and got a database query builder not converted to string error
ModelA::join(DB::connection('mysql2')->table('table1'), 'modela.id','table1.id')->get();
Eloquent does not support joining data from different connections.
If both databases are stored on different servers, the only thing you can do is to fetch relevant data from both connections and then join it programatically.
Joining data from different databases is possible if both databases are stored on the same server. In order to do that, you'll need to prefix table names with database names. With Eloquent, you'd additionally need to use the same connection to access both databases.
Then, it could look like this:
ModelA::join('database2.table1', 'modela.id', 'database2.table1.id')->get()

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.

Can I join multilpe tables from multiple database across different servers?

Is it possible to write join query between multiple databases across different servers?
Is it possible?
The answer to your question is, Yes it is possible.
But since you have not specified any particular situation you are facing,it is not possible to give a direct answer.
In MySQL it can be done using The FEDERATED Storage Engine.
However I suggest you go through following SO questions
Join tables from two different server
Joining tables across multiple servers
MySQL Cross Server Select Query
MySQL — join between tables in 2 different databases?
How To Left Join 2 Tables On 2 Different Databases?
Connecting multiple database and join query across database in php

MySQL Querying Multiple Databases

I have multiple databases and I need to search for a value in a table and set it to NULL in all instances across all databases.
I know how to query individual databases after "USE 'DBName'" is called to select a particular database.
How do I loop all databases?
Use the SHOW DATABASES query to list all databases.
You don't need to USE the database to query its tables:
SELECT something FROM databasename.sometable

How can I compare a mysql table between two databases and update the differences efficiently?

Here is the setup, I have multiple online stores that I would like to use the same product database. Currently they are all separate, so updating anything requires going through and copying products over, it is a giant pain. What I would like to do is create a master product database that every night, each site will compare its database with, and make updates accordingly.
The idea is one master database of products that will be updated a few times a day, and then say at 2:00 AM, a cron job will run pulling the updates to the individual websites.
Just a few more details on the database, there is one table 'products' that needs to be compared, but it also needs to look at table 'prodcuts_site_status' to determine the value for the products status for each given site, so I can't simply dump the master table and re-important it into the site databases.
Creating a php script to go row by row and compare and update would be easy enough, but I was hoping there existed a more elegant/efficient solution in mysql. Any suggestions?
Thanks!
To sum up you could try 3 different methods:
use SELECT ... INTO OUTFILE and then LOAD DATA INFILE from MySQL Cross Server Select Query
use the replication approach described here Perl: How to copy/mirror remote MYSQL table(s) to another database? Possibly different structure too?
use a FEDERATED storage engine to join tables from different servers http://dev.mysql.com/doc/refman/5.0/en/federated-storage-engine.html

Categories