zangetsu
zangetsu

Reputation: 127

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");
    }
});

Upvotes: 0

Views: 637

Answers (1)

LoïcR
LoïcR

Reputation: 5039

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.

Upvotes: 1

Related Questions