简体   繁体   中英

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":"Peter"}]

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM