I\'m trying to create a json object from MySQL results, but not getting the result I need.
Here is the PHP
$json = array();
$result = m
I guess the correct way to do this would be:
$json = array();
$result = mysqli_query ($connection, $query);
while($row = mysqli_fetch_array ($result))
{
$bus = array(
'latitude' => $row['lat'],
'longitude' => $row['lng'],
'icon' => './images/' . $row['busColor'] . '.png'
);
array_push($json, $bus);
}
$jsonstring = json_encode($json);
echo $jsonstring;
die();
you output your json by hand and then you call json_encode on an empty array() - $json
json_encode() outputs [] on you pass an empty array so your last [] comes from here
$jsonstring = json_encode($json);
echo $jsonstring;
Edit: More about json_encode json_encode php manual
You start by defining an array.
You then generate some JSON manually.
You then convert the array to JSON and output it.
Replace all the echo statements in and at the edge of your while loop with code to generate an associative array containing the data, and then insert it into $json.