Page 1 of 1
Merging MySQL query results
Posted: Mon Jun 02, 2008 6:02 pm
by matrix64
Hi,
I have data horizontally partitioned over several MySQL servers. After I connect and run a query on each server, I'd like to merge the results they give me and use that data in a loop to fill a table with data, reverse sorted by a PRIMARY KEY. I presume this would be done with arrays.
I'm still quite new to PHP and I'd like some help solving this problem.
Thank you.
Re: Merging MySQL query results
Posted: Mon Jun 02, 2008 6:04 pm
by Benjamin
Code: Select all
$result = array_merge($array1, $array2, $array3);
Re: Merging MySQL query results
Posted: Mon Jun 02, 2008 6:53 pm
by Eran
You might be better off performing a UNION to combine all the different datasets into one at the database level. You can then apply ordering and grouping clauses on the entire dataset.
Re: Merging MySQL query results
Posted: Mon Jun 02, 2008 7:00 pm
by matrix64
astions wrote:Code: Select all
$result = array_merge($array1, $array2, $array3);
Indeed, but:
1) How do I merge the entire result set and not just the first line?
2) How do I get this new array to do the same as mysqli_fetch_row in a loop?
pytrin wrote:You might be better off performing a UNION to combine all the different datasets into one at the database level. You can then apply ordering and grouping clauses on the entire dataset.
The datasets are on different servers. Federated tables are not an option.
Re: Merging MySQL query results
Posted: Mon Jun 02, 2008 7:53 pm
by Benjamin
I recommended array_merge assuming you already have all 3 complete result sets stored in arrays. You can use $record = array_shift($array); to pull a record off the array stack or you can loop through them with foreach(). Have a look at the array functions for details. Without knowing more that's about all I can tell you.
http://us2.php.net/manual/en/ref.array.php