Json_encode PHP中的许多数据从数据库到Jquery AJAX

Json_encode PHP中的许多数据从数据库到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");
    }
});

我花了一天的时间研究并试图从PHP到Jquery的AJAX显示一个json_encoded多维数组,直到 我尝试从数据库表中的4200多行中仅获取10行。 是不可能将这些数千行作为json_encoded数组发送到ajax? 或者我的代码有什么问题,还是应该考虑和调整? 请不要讨厌,只是帮忙。 p>

cases.php: p>

  header('Content-Type:application / json'); \  ninclude('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(); 
 
而($ row = mysqli_fetch_assoc($ cases_result)){
 $ cases_res [] = $ row; 
} 
 
echo json_encode($ cases_res); 
  code>   pre> 
 
 

JS: p>

  $。ajax({
 type:“GET”,
 url:“cases.php”,
  success:function(data){
 alert(“success”); 
 alert(data [0] .data.case_id); 
},
 complete:function(data){
 alert(“complete”)  ); 
},
错误:函数(数据){
 alert(“错误”); 
} 
}); 
  code>  pre> 
  div>

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.