1

I'm trying to fix up an old script which used to always work but now gives an empty response; I have no idea why. I have checked that the while loop is entered, num_of_rows = 100, yet I don't get any response.

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

Output :

1234444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444
11
  • The response to my get request is empty. Commented Sep 2, 2016 at 20:53
  • I am sending a GET request to this PHP script. The response is EMPTY Commented Sep 2, 2016 at 20:55
  • Basically nothing is echoed back, does that make sense? Commented Sep 2, 2016 at 20:58
  • just use the code of:-eval.in/634313. also check this file by running directly that json data is coming or not? Also add error_reporting(E_ALL);ini_set('display_errors',1); just after <?php. i forgot to add that Commented Sep 2, 2016 at 21:10
  • @Anant I added your code, I posted the screenshot of what I'm getting Commented Sep 2, 2016 at 21:21

1 Answer 1

1

Code seems to be working , but a better approach will be:-

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

Since you said that it's something related to utf-8 issue.

This link is what you found useful:-

Why would json_encode returns an empty string

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.