食谱书-PHP混乱连接3个表,未显示预期内容

食谱书-PHP混乱连接3个表,未显示预期内容,php,mysql,join,Php,Mysql,Join,我正在写一本食谱。我的数据库表如下所示: 表“recipes”包含以下列:id、名称 表“成分”有以下列:id、名称、测量值\u id 表“配方含有成分”有以下列:id、配方id、成分id、成分数量、单位id 表“单位”有以下列:id、名称、度量值\u id 其想法是,用户将能够插入配方的成分,选择如何测量(体积、质量、长度…),然后,它将显示该测量的单位列表;例如,如果我插入鸡肉,并将其选择为“质量”,下次引入鸡肉配料时,它将显示用于测量质量的单位列表 无论如何。。。。。目前,我正在尝试显

我正在写一本食谱。我的数据库表如下所示:

  • 表“recipes”包含以下列:id、名称
  • 表“成分”有以下列:id、名称、测量值\u id
  • 表“配方含有成分”有以下列:id、配方id、成分id、成分数量、单位id
  • 表“单位”有以下列:id、名称、度量值\u id
其想法是,用户将能够插入配方的成分,选择如何测量(体积、质量、长度…),然后,它将显示该测量的单位列表;例如,如果我插入鸡肉,并将其选择为“质量”,下次引入鸡肉配料时,它将显示用于测量质量的单位列表

无论如何。。。。。目前,我正在尝试显示一个包含配方名称的表格,以及包含配料名称、数量和单位名称的配料列表

如果我在mysql中按照

SELECT recipes_have_ingredients.recipe_id
     , ingredients.name
     , recipes_have_ingredients.ingredient_amount
     , units.name 
  FROM recipes_have_ingredients 
  JOIN ingredients 
    ON recipes_have_ingredients.ingredient_id = ingredients.id 
  JOIN units 
    ON recipes_have_ingredients.unit_id = units.id 
 WHERE recipe_id = 2
它显示:

+-----------+-------+-------------------+----------+
| recipe_id | name  | ingredient_amount | name     |
+-----------+-------+-------------------+----------+
|         2 | onion |                 1 | kilogram |
|         2 | milk  |                30 | litre    |
+-----------+-------+-------------------+----------+
这就是我想要的,但是,如果我尝试创建一个函数,它是:

public function display_ingredients_for_recipe($recipe_id = ':recipe_id') {
    include 'includes/db_connection.php';   
    try {

        $sql = "SELECT recipes_have_ingredients.recipe_id, ingredients.name, recipes_have_ingredients.ingredient_amount, units.name FROM recipes_have_ingredients JOIN ingredients ON recipes_have_ingredients.ingredient_id=ingredients.id JOIN units ON recipes_have_ingredients.unit_id=units.id WHERE recipe_id=:recipe_id";

          $results = $conn->prepare($sql);
          $results->bindParam(':recipe_id', $recipe_id, PDO::PARAM_INT);
          $results->execute();
         } catch(PDOException $e) {
            echo 'Error: ' . $e->getMessage() . '<br />';
            return array();
        }
          return $results->fetchAll(PDO::FETCH_ASSOC);
        }
我不知道我做错了什么。。。。为什么如果我在mysql上执行查询,它会选择我想要的内容,但是如果我尝试调试成分,我就无法显示成分名称和单元名称


任何帮助都将不胜感激。

因为您有两个同名字段(具有讽刺意味的是,这两个字段碰巧也是“name”)。PHP不能在一个数组中有两个相同的键,因此它们会覆盖。如果重命名其中一个(或两个)字段,它应该可以工作


嗯,一公斤洋葱和30升牛奶。这是我的烹饪风格。它只是虚拟数据^^哦哦哦哦!!!!!我现在明白了!!!!谢谢你,埃里克!!:说真的,我已经花了大约一个小时试图发现我做错了什么。
public function display_recipes() {
    include 'includes/db_connection.php';
    try {

        $sql = "SELECT id, name FROM recipes";

        $results = $conn->prepare($sql);
        $results->execute();
    } catch (PDOException $e) {
        echo 'Error: ' . $e->getMessage() . '<br />';
        return array();
    }
    return $results->fetchAll(PDO::FETCH_ASSOC);
}

 $ingredients = $recipeObj->display_ingredients_for_recipe($recipe['id']);
 echo '<pre>';
 print_r($ingredients);
 echo '</pre>'; die();
Array
(
    [0] => Array
        (
            [recipe_id] => 1
            [name] => gram
            [ingredient_amount] => 350
        )

)
SELECT recipes_have_ingredients.recipe_id, ingredients.name AS ingredient_name, recipes_have_ingredients.ingredient_amount, units.name AS unit_name ...