$ result_array仅返回一行

时间:2018-06-20 13:34:28

标签: php

下面的查询应返回多种成分,但仅返回一种成分。我在做什么错了?

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $result_array['id'] = $row['id'];
        $result_array['name'] = $row['product_name'];
        $result_array['phone'] = $row['categories_name'];
        $result_array['image'] = $row['url'];

        $sqlI = "SELECT * FROM `ingredients` inner join products_ingredients on ingredients.id=products_ingredients.idIngredient WHERE idProduct=".$row['id']."";
        $resultI = $conn->query($sqlI);

        if($resultI->num_rows > 0) {
            while($rowI = $resultI->fetch_array()) {
                $result_array['ingredients'] = $rowI['ingredient_name'];

            }
        } 
    array_push($products, $result_array);   
    }
}

当我使用echo时,它会显示所有行

 $sqlI = "SELECT * FROM `ingredients` inner join products_ingredients on ingredients.id=products_ingredients.idIngredient WHERE idProduct=".$row['id']." ORDER BY ingredient_name ASC";
    $resultI = $mysqli->query($sqlI);

    if($resultI->num_rows > 0) {
        while($rowI = $resultI->fetch_assoc()) {
            echo $rowI['ingredient_name'].' <a href="removeProdIngred.php?id='.$rowI['id'].'">X</a><br />';
        }
    }

输出

[1] => Array
        (
            [id] => 2
            [name] => Supa de rosii (350 ml)
            [phone] => Supe
            [image] => https://restaurant.onlinebusiness.university/img/products/2/2.jpg
            [ingredients] => ceapa
        )

1 个答案:

答案 0 :(得分:1)

您需要在[]数组之后添加$result_array['ingredients'],然后它将所有Ingredient_name名称存储到数组中。

$result_array['ingredients'][] = $rowI['ingredient_name'];