Jump to content

Merging 2 mysql resultant arrays


chrisfane

Recommended Posts

im currently executing sql queries on two seperate mysql servers.  i take back each of these results into individual tables to display them. 

 

What i wanted to do was perform a merge of the two arrays, so that i could order the results by my "TimeStamp" field.

 

When i try to perform a basic " $mergedresult = array_merge($result, $isaresult); " i get the following.

 

"

Warning: array_merge() [function.array-merge]: Argument #1 is not an array in C:\Program Files\xampplite\htdocs\seatpub.php on line 164

 

Warning: array_merge() [function.array-merge]: Argument #2 is not an array in C:\Program Files\xampplite\htdocs\seatpub.php on line 164  "

 

Im rather new to this and so havent delt with arrays before, but im under the impression my results from the sql queries, are Assoc arrays.  can these be merged ? do i need to use an alternate php function ?

 

 

Thanks for any help

 

Chris

Link to comment
Share on other sites

you need to create an array from each result set

 

I suspect you are doing someting like...

 

<?php
$result = mysql_query('SELECT...');
$isaresult = mysql_query('SELECT...');

$mergedresult = array_merge($result, $isaresult);
?>

 

If so you have missed out the array formation...

 

<?php
$result = mysql_query('SELECT...');
$isaresult = mysql_query('SELECT...');
$resultarr = array();
while($row = mysql_fetch_assoc($result))
{
foreach($row as $key => $val)
{
  $resultarr[$key][] = $val;
}
}

$isaresult = array();
while($row = mysql_fetch_assoc($result))
{
foreach($row as $key => $val)
{
  $isaresult[$key][] = $val;
}
}

$mergedresult = array_merge($resultarr, $isaresultarr);
?>

 

the field names present in the two datasest should be the same - otherwise you may give yourself a bit of a headache!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.