Php 如何从数据库中回显多个结果
如何回显多个结果。目前,它显示配方名称,但我希望能够从数据库中回显步骤。因此,数据库中的字段将是步骤1Php 如何从数据库中回显多个结果,php,Php,如何回显多个结果。目前,它显示配方名称,但我希望能够从数据库中回显步骤。因此,数据库中的字段将是步骤1 <?php if (isset($_POST['search'])) { $ingredient1 = $_POST['dropdown1']; $ingredient2 = $_POST['dropdown2']; $ingredient3 = $_POST['dropdown3']; $recipes = m
<?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>';
?>
这引发了一个错误PHP分析错误:语法错误,第97行的\PDC3\sites\c\cupboard2stomach.com\public\u html\PHP\get.PHP中出现意外的“,”,应为“]”
echo $recipe['name'].'<br />';
echo $recipe['step1'].'<br />';
echo$recipe['name']。
;
echo$recipe['step1'].
;
没有错误,但不显示结果
echo $recipe['name','step1'].'<br />';
echo$recipe['name','step1']。
;
这是食谱表
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>';
echo';
而($recipe=mysql\u fetch\u assoc($recipes)){
echo$recipe['name']。
;
echo$recipe['step1'].:','
';
echo$recipe['step2'].:','
';
echo$recipe['step3'].:','
';
}
}
回声';
这些是我返回的结果您的查询只返回名称,并且每次出现
name
时只返回一次
在while循环中,执行一个新的查询以查找每个配方的步骤
name
,并循环执行这些步骤以分别进行响应 我在查询的选择字段中看不到step1
列。您需要向我们显示错误以及mysql表的外观。您的sql查询似乎只选择了一列--“name”Add mysql\u real\u escape\u string,比如,$ingredient1=mysql\u real\u escape\u string($\u POST['dropdown1']);为了防止sql注入。@web2students.com谢谢,我会这么做的!echo$recipe['step1'].;这是错的吗?因为当我运行它时,结果是“豆在吐司上和步骤1”,它实际上并没有显示步骤1。。。而不是使用DISTINCT is GROUP BY,它仍然只显示单词step 1I,我认为您必须在查询的in条件中的成分变量周围使用撇号。(当它不仅仅是数值的时候)@bestprogrammerintheworld我想我已经做到了,我已经编辑了这篇文章来显示结果。
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`