duanrui3480 2018-06-20 13:34
浏览 86
已采纳

$ result_array只返回一行

The query below should return multiple ingredients, but it only returns one ingredient. What am I doing wrong?

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);   
    }
}

And when I use echo it displays all the rows

 $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 />';
        }
    }

The output

[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条回答 默认 最新

  • dongzhimin2231 2018-06-20 13:39
    关注

    You need to add [] after $result_array['ingredients'] array, then it will store all ingredient_name name into array.

    $result_array['ingredients'][] = $rowI['ingredient_name'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献