Eros Scoppiato Eros Scoppiato - 1 year ago 60
jQuery Question

Can't read json elements from mysql query via ajax

I need to add some geo-marker to my map.
the markers are in my mysql table on
but my JavaScript says [object Object] every time i try...

here my php code:

$query = "SELECT latit, longit FROM segnalazioni";
$result = mysql_query($query);
$rows = array();
while ($row = mysql_fetch_assoc($result)){
$rows[] = $row;
echo json_encode($rows);

it returns:


here my JavaScript:

function addMarker(mapobj) {
$.getJSON( "", function( data ) {
var items = [];
$.each( data, function( key1 , val1 ) {
items.push( "<li id='" + key1 + "'>" + val1 + "</li>" );
//next todo:
//mapobj.marker([latit, longit]).addTo(map).bindPopup("hi");
$( "<ul/>", {
"class": "my-new-list",
html: items.join( "" )
}).appendTo( "body" );

and on the end of my [body] i can see only:

[object Object]
[object Object]
[object Object]

Answer Source

Use this

$.each(result, function(key, value){
        $.each(value, function(key, value){
            console.log(key, value);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download