开发者

How do I get all the rows from a mysql table, send json, iterate through array and display?

I have this PHP code:

$result = mysql_query("SELECT banner FROM banners ") or die(mysql_error()); 
 $somethings = array(); 
while ($row = mysql_fetch_assoc($result)) { 
    $somethings[] = $row;
}

  print json_encode($somethings);   exit;

which gives me this json response:

[{"name":"john"},{"name":"P开发者_运维技巧eter"}]

and I have this JavaScript:

$(document).ready(function(){
function getdata(data) {
$.each($data, function(key, value) {jQuery('#boxes').append('==='+value); }); 
}
$.getJSON('editx2.php',{ 'case':9,'readonly': 'yes' }, getdata);    
});

which dispays this:

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

I have two rows in the database. When the array is in this format:

{"name":"john","name":"peter"} 

it works, but this fails:

[{"name":"john"},{"name":"Peter"}]


Instead of

jQuery('#boxes').append('==='+value); 

use

jQuery('#boxes').append('==='+value.name);

which will print the name.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜