代码之家  ›  专栏  ›  技术社区  ›  Alk

Php脚本空响应获取数据

  •  1
  • Alk  · 技术社区  · 9 年前

    我正在尝试修复一个旧脚本,它以前总是有效,但现在给出了一个空响应;我不知道为什么。我已经检查了while循环是否已进入, num_of_rows = 100 ,但我没有得到任何回应。

    <?php
    
    $response = array();
    
    
    $conn=mysqli_connect("localhost", "***", "***","***");
    
    
    // get all gamelists from gamelists table
     $result = mysqli_query($conn,"SELECT * FROM `abcd`");
    
    
    // check for empty result
    if (mysqli_num_rows($result) > 0) {
    
    
    $response["gamelist"] = array();
    
    while ($row = $result->fetch_array()) {
        // temp user array
    
        $gamelist = array();
        $gamelist["id"] = $row["id"];
        $gamelist["ques"] = $row["ques"];
        $gamelist["odp_a"] = $row["odp_a"];
        $gamelist["odp_b"] = $row["odp_b"];
        $gamelist["odp_c"] = $row["odp_c"];
        $gamelist["odp_d"] = $row["odp_d"];
        $gamelist["comment"] = $row["comment"];
        $gamelist["correctanswer"] = $row["correctanswer"];
        $gamelist["commentfirst"] = $row["commentfirst"];
    
    
    
    
    
        // push single gamelist into final response array
    
        array_push($response["gamelist"], $gamelist);
    }
    // success
    $response["success"] = 1;
    
    // echoing JSON response
    echo json_encode($response);
     } else {
    // no gamelists found
      $response["success"] = 0;
    $response["message"] = "No gamelists found";
    
    // echo no users JSON
    echo json_encode($response);
     }
    ?>
    

    enter image description here

    输出:

    1234444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444
    
    1 回复  |  直到 9 年前
        1
  •  1
  •   Community CDub    8 年前

    代码似乎有效,但更好的方法是:-

    <?php
    
    $response = array();
    
    
    $conn=mysqli_connect("localhost", "***", "***","***");
    
    if($conn){
        // get all gamelists from gamelists table
        $result = mysqli_query($conn,"SELECT * FROM `abcd`");
    
        if($result){
            // check for empty result
            if (mysqli_num_rows($result) > 0) {
                while ($row = $result->fetch_array()) {
                    $response["gamelist"][] = $row;
                }
                // success
                $response["success"] = 1;
    
                // echoing JSON response
                echo json_encode($response); exit;
            } else {
                // no gamelists found
                $response["success"] = 0;
                $response["message"] = "No gamelists found";
    
                // echo no users JSON
                echo json_encode($response);exit;
            }
        }else{
            echo "db query error".mysqli_error($conn); exit;
        }
    }else{
        echo "db connection error".mysqli_connect_error(); exit;
    }
    ?>
    

    此链接非常有用:-

    Why would json_encode returns an empty string