How to echo multiple results from database
How do I echo multiple results. Currently this displays recipe names but I want to be able to echo the steps from the database as well. So the field in the database would be step 1.
<?php
if (isset($_POST['search'])) {
$ingredient1 = $_POST['dropdown1'];
$ingredient2 = $_POST['dropdown2'];
$ingredient3 = $_POST['dropdown3'];
$recipes = mysql_query("
SELECT DISTINCT `name`
FROM `recipe` r
INNER JOIN `recipe_ingredients` ri
ON r.id = ri.recipe_id
WHERE ri.ingredient_id IN (".$ingredient1.",".$ingredient2.",".$ingredient3.")
");
echo '<section id="results">';
while ($recipe = mysql_fetch_assoc($recipes)) {
echo $recipe['name'].'<br />';
}
}
echo '</section>';
?>
This threw up an error. : PHP Parse error: syntax error, unexpected ',', expecting ']' in PDC3sitesccupboard2stomach.compublic_htmlphpget.php on line 97
echo $recipe['name'].'<br />';
echo $recipe['step1'].'<br />';
No error but doesn't display the results.
echo $recipe['name','step1'].'<br />';
This is the recipe table
echo '<section id="results">';
while ($recipe = mysql_fetch_assoc($recipes)) {
echo $recipe['name'].'<br />';
echo $recipe['step1'].':','<br />';
echo $recipe['step2'].':','<br />';
echo $recipe['step3'].':','<br />';
}
}
echo '</section>';
These are the results I get back
SELECT r.`name`, r.`step1`
FROM `recipe` r
INNER JOIN `recipe_ingredients` ri
ON r.id = ri.recipe_id
WHERE ri.ingredient_id IN ('".$ingredient1."', '".$ingredient2."', '".$ingredient3."')
GROUP BY r.`name`
Your query only returns the name, and only once for each time name
occurs.
Inside your while loop, execute a new query to find the steps for each recipe name
, and loop through the steps to echo them separately.
上一篇: PHP表单验证
下一篇: 如何从数据库中回显多个结果