2013-05-04 31 views
0

如何回顯多個結果。目前這顯示配方名稱,但我希望能夠迴應數據庫中的步驟。所以在數據庫中的字段將是第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>'; 
    ?> 

這引發了一個錯誤。 :PHP解析錯誤:語法錯誤,意外 '',期待在\ PDC3 \網站 ']' \ C \ cupboard2stomach.com \的public_html \上線PHP \ get.php 97

echo $recipe['name'].'<br />'; 
echo $recipe['step1'].'<br />'; 

沒有錯誤,但沒有按不顯示結果。

echo $recipe['name','step1'].'<br />'; 

enter image description here 這是配方表

enter image description here

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>'; 

這些都是結果我回來

+3

我在查詢的選擇字段中看不到'step1'列。 – Rikesh 2013-05-04 16:56:17

+1

您需要向我們展示錯誤以及您的mysql表的外觀。 – 2013-05-04 16:57:20

+0

你的SQL查詢似乎只選擇一列 - 'name' – MrVimes 2013-05-04 16:59:00

回答

1
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` 
+0

echo $ recipe ['step1']。'
'; 這是錯誤的嗎?因爲當我運行它時,結果是「豆吐司和步驟1」,它實際上並沒有向我展示step1。 – 2013-05-04 17:03:25

+0

我做了編輯...而不是DISTINCT使用GROUP BY – 2013-05-04 17:06:33

+0

這仍然只顯示單詞步驟1 – 2013-05-04 17:08:05

0

你的查詢只每次返回名稱,只有一次發生name

在你的while循環中,執行一個新查詢來查找每個配方name的步驟,並循環執行步驟以分別回顯它們。

相關問題