简体   繁体   中英

Retrieving JSON response in jQuery

I'm trying to retrieve the response of this script:

<?php
require_once("db_connect.php"); 
$get = mysql_query("SELECT name FROM main");
$genres;
while ($row = mysql_fetch_array($get)) {
    $genres[] = $row['name'];
}
echo json_encode($genres); 
?>

And get it back in javascript and store it in a variable, so when I do variable[i] in a loop I'll get the ith genre name.

It appears that you have the PHP script setup correctly. Take a look at jQuery's getJSON() method.

$.getJSON('your_script.php', function(genres) {
    // here genres[i] is your i'th genre from the list
});

Try this one -

 require_once("db_connect.php");  
 $result = mysql_query("SELECT * FROM main");

    $json = array();
    $total_records = mysql_num_rows($result);

    if($total_records >= 1){
      while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
        $json[] = $row;
      }
    }

    echo json_encode($json);

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