开发者

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

开发者 https://www.devze.com 2023-03-20 05:12 出处:网络
I have this PHP code: $result = mysql_query(\"SELECT banner FROM banners \") or die(mysql_error()); $somethings = array();

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

精彩评论

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