How to query multiple tables with multiple selects MySQL
- by brybam
I'm trying to write a php function that gets all the basic food data(this part works fine in my code)
Then grabs all the ingredients related to the ids of the selected items from the query before.
Anyone have have any idea why my second array keeps coming back as false?
I should be getting 1 array with the list of foods (this works)
Then, the second one should be an array with all the ingredients for all the foods that were previously selected...then in my code im planning to work with the array and sort them with the proper foods based on the ids.
function getFood($start, $limit) {
$one = mysql_query("SELECT a.id, a.name, a.type, AVG(b.r) AS fra, COUNT(b.id) as tvotes FROM `foods` a LEFT JOIN `foods_ratings` b ON a.id = b.id GROUP BY a.id ORDER BY fra DESC, tvotes DESC LIMIT $start, $limit;");
$row = mysql_fetch_array($one);
$qry = "";
foreach ($row as &$value) {
$fid = $value['id'];
$qry = $qry . "SELECT ing, amount FROM foods_ing WHERE fid='$fid';";
}
$two = mysql_query($qry);
return array ($one, $two);
}