How to implement a data loop on ajax?

function getTableKredit(){ debugger; var id; $.ajax({ url:"credit-table.php", cache: false, dataType: "json", success: function(data){ //как виводить }, error: function(data){ } }) } $queryCredit = "SELECT * FROM `credit`"; $resCredit = mysqli_query($conection, $queryCredit); while($row=mysqli_fetch_array($resCredit)) { $output = [ "id" => $row['id'], "description" => $row['description'], "grn" => $row['grn'], "pln" => $row['pln'], "eur" => $row['eur'], ]; echo json_encode($output); } 
  • You output incorrect json (because json_encode is output more than 1 time), the function "success" in js will not be called because of this. Try to understand how php / js / json works, it will be clearer. - Sanya_Zol

2 answers 2

You get an incorrect json. It is necessary to serialize all data at once, but not in parts. Accumulate data in a while and send everything at once.

 $output = []; while($row=mysqli_fetch_array($resCredit)) { $output[] = [ "id" => $row['id'], "description" => $row['description'], "grn" => $row['grn'], "pln" => $row['pln'], "eur" => $row['eur'], ]; } echo json_encode($output); 

    Slowly get out of the stone age,

     $queryCredit = "SELECT * FROM `credit`"; $output = $conection->query($queryCredit)->fetch_all(MYSQLI_ASSOC); echo json_encode($output);