I have a php command that like this.
$query = "SELECT * FROM $table WHERE user_id = 0";
$results = mysql_query($query)or die(mysql_error());
while($row = mysql_fetch_assoc($results)){
$values []= $row;
}
echo json_encode($values);
This creates a json array like this
[{"id":"1","name":"amazon.com","url":"http:\/\/amazon.com","time":"5","credits":"0","user_id":"0"},{"id":"2","name":"google.com","url":"http:\/\/google.com","time":"4","credits":"0","user_id":"0"}]
This json array than becomes part of a ajax response Text on a different page.
var ajaxd = ajax();
ajaxd.onreadystatechange = function(){
if(ajaxd.readyState == 4){
var nse = eval('('+ajaxd.responseText+')');
alert(nse['url']);
}
}
The problem is.. The alert is always being undefined. What am I doing wrong?
the outermost containing structure in the resulting json is a list; this makes sense because most data returned from a database will be a bunch of rows. You probably want to access the ‘url’ of the first row:
Or maybe you really need to do something with every row: