Fetch data from two databases using PHP MySQL mysqli - php

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.

Related

Many-to-Many Relationship Query

Right now I'm trying to query a table that holds the id of two tables that holds the id to two other tables (where one of these holds the id to other tables)
Hell, I know.
As I am a beginner in SQL, how do I go about efficiently querying this to get all the details of each table? As of now i only see a very long set of Select statements. Here is how the tables are set up below
I am not sure what you are asking.
If you need a SELECT command it would have 2 INNER joins. But, if you are asking to get all the info (and children) then it would need more INNER joins. OR if you are using a persistent client for your application.... I just don't know. Because it is incomplete data to do it.

Check if a value exists in Column Mysqli php

I developing a chat app, in that i need to connect between two users in my database and the point is i dont want to connect users that already connected earlier. So i store a column to store the already connected users which will be in the format: id1|id2|id3|.....|id50.
First i thought of selecting a user and get his id and then fetch the user's connected column and then by php check if id exists in his column, thus denying that chat. But it makes lot more complicated.
Can anyone recommend a strategy to make this simple by using only MySQL Queries, i am not advanced in writing nested or join queries and the don't know all pre-defined functions that exist in MySQL.
Can anyone make a suggestion?
SELECT * FROM table WHERE column IN (this,that,these,those,em)
if statement returns not empty value, sure, user exists.
I wrote example where you can check many users at once. Just implode your id-user-list and quire em in parenthesise..
Thus, u'll need to parse returned array to see who succeeded in selecting, who not. (compare two arrays)
Happy coding..

Copy all information from one table and insert it into another table on different server using PHP

I want to copy everything from a table on my local server and insert it into a table on a remote server.
Something like
INSERT INTO table2
SELECT * FROM table1;
How can I adapt this for 2 tables on different servers and databases?
Well, if you want to use PHP, then you could do something like querying everything from one table, simply like SELECT * FROM table, and then iterating over your table with a while loop, inserting one record at a time to the new table. I assume you know some PHP, for this answer to help you. You can't do it with one SQL statement atleast, that's for sure.
Please look at this StackOverflow thread: SQL Insert into … values ( SELECT … FROM … ). There are discussed some compatibility issues across various database engines, too.
It should answer your question quite good as long as it is within a single database.
For copies between different database instances have look at backup & restore, export & import mechanisms or at seperate copy scripts in php, python, etc. using either native or ODBC database drivers.

Connecting and querying two Mysql databases simultaneously with 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.

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