When I had problems joining these two tables, I was able to do exactly what I wanted by opening both remote databases at the same time. MySQL 5.6 (php 7.1) and other MySQL 5.1 (php 5.6)
//Open a new connection to the MySQL server $mysqli1 = new mysqli('server1','user1','password1','database1'); $mysqli2 = new mysqli('server2','user2','password2','database2'); //Output any connection error if ($mysqli1->connect_error) { die('Error : ('. $mysqli1->connect_errno .') '. $mysqli1->connect_error); } else { echo "DB1 open OK<br>"; } if ($mysqli2->connect_error) { die('Error : ('. $mysqli2->connect_errno .') '. $mysqli2->connect_error); } else { echo "DB2 open OK<br><br>"; }
If you see these two OKs on the screen, then both databases are open and ready. Then you can begin to fulfill your requests.
For your specific question, I will do something like the first selection in the database A of all 10-year-old children, then match them with the colors by the identifier from database B. This should work, I did not test this code on my server, but my example below this code works. You can perform an arbitrary request for any, color, age, whatever you like, even group them as you need.
$results = $mysqli1->query("SELECT * FROM DatabaseTableA where age=10"); while($row = $results->fetch_array()) { $theColorID = $row[0]; $theName = $row[1]; $theAge = $row[2]; echo "Kid Color ID : ".$theColorID." ".$theName." ".$theAge."<br>"; $doSelectColor = $mysqli2->query("SELECT * FROM DatabaseTableB where favorite_color=".$theColorID." "); while($row = $doSelectColor->fetch_assoc()) { echo "Kid Favorite Color : ".$row["favorite_color"]."<br>"; } }
I used this to switch between our programs without joining tables from remote servers, and I have no problems so far.
$results = $mysqli1->query("SELECT * FROM video where video_id_old is NULL"); while($row = $results->fetch_array()) { $theID = $row[0]; echo "Original ID : ".$theID." <br>"; $doInsert = $mysqli2->query("INSERT INTO video (...) VALUES (...)"); $doGetVideoID = $mysqli2->query("SELECT video_id, time_stamp from video where user_id = '".$row[13]."' and time_stamp = ".$row[28]." "); while($row = $doGetVideoID->fetch_assoc()) { echo "New video_id : ".$row["video_id"]." user_id : ".$row["user_id"]." time_stamp : ".$row["time_stamp"]."<br>"; $sql = "UPDATE video SET video_id_old = video_id, video_id = ".$row["video_id"]." where user_id = '".$row["user_id"]."' and video_id = ".$theID.";"; $sql .= "UPDATE video_audio SET video_id = ".$row["video_id"]." where video_id = ".$theID.";"; // Execute multi query if you want if (mysqli_multi_query($mysqli1, $sql)) { // Query successful do whatever... } } } // close connection $mysqli1->close(); $mysqli2->close();
I tried to make several joins, but since these two databases were open, I can move from one query to another just by changing the connection of $mysqli1 or $mysqli2
It worked for me, I hope this helps ... Hooray