I have the following PHP which gathers data from the DB and places each row into an array which it then places in a final array which is returned to the JavaScript:
$active = /*some SQL statement*/
$result = mysqli_query($GLOBALS['con'], $active);
$data = array();
while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)){
$value = array(
"id"=>$row["quest"],
"received"=>$row["received"]
);
array_push($data, $value);
}
return json_encode($data);
I know that the SQL statement is working and that $value
has values in it, but the array returned to the JavaScript page is always an empty string instead of a JSON object.
If I place my return statement inside my while
loop, then the data is returned:
while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)){
$value = array(
"id"=>$row["quest"],
"received"=>$row["received"]
);
array_push($data, $value);
return json_encode($data);
}
But obviously this does not return the data as I want it, after the loop has completed.
And if I change my return statement to:
return json_encode($data[1]);
Then the data is also returned.
Please help me figure this one out.