I've got a problem with the interaction between PHP and JavaScript via JSON.
The PHP-Script reads data from my database. The SQL result consists of 1 row.
name | lat | lng
Kilian | 45.1252335 | 32.2142380
It is encoded into JSON:
$query = "SELECT name, lat, lng FROM location l, user u WHERE u.email = l.email";
$result = mysqli_query ($dbc, $query) or die ('Error querying database.');
$to_encode = array();
while($row = mysql_fetch_assoc($result)) {
$to_encode[] = $row;
}
echo json_encode($to_encode);
The JavaScript program loads the JSON file from the php-script (get_locations.php):
$.getJSON('get_locations.php', function (data) {
$.each(data, function(i, name) {
alert(name.parent_level);
});
});
But there is no alert in the browser. If i inspect the sent JSON in Chrome it just says []. How can i get to the sent name, lat and lng?
Edit: The real world output of get_locations.php is []. Converting the JSON to string always resulted in [].
$to_encondeusing an index instead of [] (For some reason, json_encode does weird things with that).console.log(data)could also help.