Jump to content
Larry Ullman's Book Forums

Trying To Merge Two Arrays Just Not Working


Recommended Posts

I run a SELECT query on my mysql database. The results are to be stored in an array $ar.

I then change the SELECT statement and run it again. This time the results need to be stored in an array $ar2. These two arrays then need to be mearged into 1 for further processing. Not new to programing but new to this. Any help you can give would be greatly appreciated.
 

<?PHP
// First query run here.
$result=mysql_query($sql);

$num=mysql_num_rows($result);

$ar=array();
$i=0;
while($i < $num)
{
$ar[]=mysql_result($result,$i,"pic");
$ar[]=mysql_result($result,$i,"Title");
$ar[]=mysql_result($result,$i,"Directions");
$ar[]=mysql_result($result,$i,"Notes");
}
// Second query run here

$result=mysql_query($sql);

$num=mysql_num_rows($result);

$ar2=array();
$i=0;
while($i < $num)
{
$ar2[]=mysql_result($result,$i,"pic");
$ar2[]=mysql_result($result,$i,"Title");
$ar2[]=mysql_result($result,$i,"Directions");
$ar2[]=mysql_result($result,$i,"Notes");
}

//merge arrays into 1
$ar3=array();
$ar3=array_merge($ar,$ar2);  //array_merge($array1, $array2);
?>
Link to comment
Share on other sites

I decided to go with the SELECT with a UNION ALL.

I have used the first statements many time and it works when, but when I ad the "UNION ALL" or just the "UNION" statement it stops working. There is no error messages the script runs to the last else "No Records"  All the variables are preset and working. I have tested everything up to the SELECT statements.

 

$result=mysql_query($sql);
print_r($result);         prints out  "Rescorce#3"

 

The script runs all the way through to the ending else "No Records". The problem is when it gets to the SELECT its like the SELECT statements never ran?


 

$sql="SELECT * FROM recipes WHERE Cat='$cat'and Eggs='$Eggs' and Fruitose='$Fruitose' and Gluten='$Gluten' and Lactose='$Lactose' and Nuts='$Nuts' and Soy='$Soy' and LowFat='$LowFat' and Diabetice='$Diabetice' and Vegan='$Vegan'
UNION ALL
SELECT * FROM recipes WHERE Cat='$cat'and Eggs='$Eggs2' and Fruitose='$Fruitose2' and Gluten='$Gluten2' and Lactose='$Lactose2' and Nuts='$Nuts2' and Soy='$Soy2' and LowFat='$LowFat2' and Diabetice='$Diabetice2' and Vegan='$Vegan2'";  
Link to comment
Share on other sites

 Share

×
×
  • Create New...