-1

how can I combine the results from two separate MYSQL databases? The databases are on separate servers, they contain identical tables but different data.

I would like to get a list of items from both databases at once but sorted by date.

For example

$result = mysqli_query ($conn, "SELECT * FROM tab ORDER BY date");
$result2 = mysqli_query ($conn2, "SELECT * FROM tab ORDER BY date");

something like that

while ($row = mysqli_fetch_array ($result && $results2)) {...}

I know this is incorrect but just to visualize what I want to achieve.

If this is not possible, how else can I achieve this result?

Shadow
  • 33,525
  • 10
  • 51
  • 64
Prokul
  • 19
  • 1
  • 1

1 Answers1

0

You can't. But you can do it in PHP. Once you have both results you would have to sort it in code.

$query1 = mysqli_query ($conn1, "SELECT * FROM tab");
$query2 = mysqli_query ($conn2, "SELECT * FROM tab");

$rows1 = mysqli_fetch_all($query1);
$rows2 = mysqli_fetch_all($query2);

$allRows = array_merge($rows1, $rows2);

Note that $allRows will not be sorted. You can sort it using usort() as described in this other SO question, adapted to your date format.

istepaniuk
  • 4,016
  • 2
  • 32
  • 60