duanquan1207 2019-01-09 10:41
浏览 78
已采纳

数据源在第二个while循环中不可用

I have the following php code:

$user_id = $user["id_user_key"];
$stmt = $db->prepare("CALL spGetUserProducts(?)");
$stmt->bind_param('i', $user_id);
$stmt->execute();
$result = $stmt->get_result();

$data = array();

while($row = $result->fetch_assoc()) {
    $row_array = array();
    $row_array["id"] = $row["id"];
    $row_array["pname"] = $row["pname"];
    $row_array["picon"] = $row["picon"];
    $row_array["menuItems"] = array();
    $product = $row["id"];
    //loop
    $result_opt = $db->query("CALL spGetUserProductViews($user_id, $product)");
    while ($opt_fet = $result_opt->fetch_assoc()) {
        $row_array["menuItems"][] = array(
            "id" => $opt_fet["id"],
            "vname" => $opt_fet["vname"],
            "isheader" => $opt_fet["isheader"]
        );
    }
    array_push($data, $row_array);
}

$stmt->close();

echo json_encode($data);

The first loop can get a hold of $db, in other words: the first prepared statement is being excecuted and gives me results. The second one:

$result_opt = $db->query("CALL spGetUserProductViews($user_id, $product)");

gives me false. When I try this statement outside the loop, it does work.

Any thoughts ont his?

  • 写回答

1条回答 默认 最新

  • dtmm0148603 2019-01-09 13:52
    关注

    I found out that mysqli can't handle two simultaneous queries because mysqli uses unbuffered queries by default. Now, I could have dived into this (for example make use of $stmt->store-result()), but I also realized that I would like to keep the load on my database to a minimum.

    My solution:

    $data = array();
    $user_id = $user["id_user_key"];
    
    //menus -> products
    $stmt = $db->prepare("CALL spGetUserProducts(?)");
    $stmt->bind_param("i", $user_id);
    $stmt->execute();
    $result = $stmt->get_result();
    
    $menus = array();
    while($row = $result->fetch_assoc()) {
        $menus[] = $row;
    }
    
    $stmt->close();
    
    //items -> views
    $stmt = $db->prepare("CALL spGetUserProductViews(?)");
    $stmt->bind_param("i", $user_id);
    $stmt->execute();
    $result = $stmt->get_result();
    
    $items = array();
    while($row = $result->fetch_assoc()) {
        $items[] = $row;
    }
    
    $stmt->close();
    
    //generate object
    //loop menus
    foreach($menus as $m){
        $row_array = array();
        $row_array["id"] = $m["id"];
        $row_array["pname"] = $m["pname"];
        $row_array["picon"] = $m["picon"];
        $row_array["menuItems"] = array();
        //loop items
        foreach($items as $i) {
            if($m["id"] == $i["id_product"]) {
                $row_array["menuItems"][] = array(
                    "id" => $i["id"],
                    "vname" => $i["vname"],
                    "isheader" => $i["isheader"]
                );
            }
        }
        array_push($data, $row_array);
    }
    
    echo json_encode($data);
    

    So now I first generate arrays out of the two objects. then I do a foreach over the menus and then over the items. When the $menu["id"] equals $items["id_product"] then the array with items for that specific menu is being generated.

    EDIT

    After the data has been pulled from the database I first have to do a check wether or not the array contains data:

    if(!empty($menus) && !empty($items)) {
        foreach ($menus as $m) {
            $row_array = array();
            $row_array["id"] = $m["id"];
            $row_array["pname"] = $m["pname"];
            $row_array["picon"] = $m["picon"];
            $row_array["menuItems"] = array();
            //loop items
            foreach ($items as $i) {
                if ($m["id"] == $i["id_product"]) {
                    $row_array["menuItems"][] = array(
                        "id" => $i["id"],
                        "vname" => $i["vname"],
                        "isheader" => $i["isheader"]
                    );
                }
            }
            array_push($data, $row_array);
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分