Php脚本空响应获取数据

时间:2016-09-02 20:46:38

标签: php sql mysqli server-side

我试图修复一个过去常常工作的旧脚本,但现在给出一个空的响应;我不知道为什么。我已检查输入了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 个答案:

答案 0 :(得分:1)

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

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

既然你说它与utf-8问题有关。

此链接是您认为有用的: -

Why would json_encode returns an empty string