简体   繁体   中英

Json_encode many data in PHP from database to Jquery AJAX

I've been spending a day researching and trying to display a json_encoded multi-dimensional array from PHP to Jquery's AJAX until I tried fetching only 10 rows from 4200+ rows in my database's table. Is it impossible to send those thousands of rows to ajax as a json_encoded array? Or is there something wrong with my code or should there be something to consider and tweak? No hate please, just help.

cases.php:

header('Content-Type: application/json');
include('connect-db.php');
$cases_sql = "SELECT * FROM cases ORDER BY case_id ASC LIMIT 10";
$cases_result = mysqli_query($conn, $cases_sql);

$cases_res = array();

while ($row = mysqli_fetch_assoc($cases_result)) {
    $cases_res[] = $row;
}

echo json_encode($cases_res);

JS:

$.ajax({
    type: "GET",
    url: "cases.php",
    success: function(data) {
        alert("success");
        alert(data[0].data.case_id);
    },
    complete: function(data) {
        alert("complete");
    },
    error: function(data) {
        alert("error");
    }
});

It's possible in fact, but json_encode become really messy when you don't have characters in UTF-8.

mysqli_query($conn, 'SET CHARACTER SET utf8');

Try with something like this and tell us if it work.

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