简体   繁体   English

在mysql中获取多个记录并将其存储到HTML表单个单个字段中的json数组显示中

[英]fetch multiple records in mysql and store it into json array display in the HTML table single single field

I am fetching multiple records from database and try to store into json array, but the only last record will get stored into array. 我正在从数据库中获取多个记录,并尝试将其存储到json数组中,但是唯一的最后一条记录将被存储到数组中。 How to store multiple data into an json array. 如何将多个数据存储到json数组中。 And I have to display this array in HTML table single single field. 而且我必须在HTML表单个字段中显示此数组。

$data11 = mysql_query($sql1);
while($row = mysql_fetch_array($data11)) {  
  $data["names"] = $row["name"];
}
echo json_encode($data);

 $.ajax({
     data:{durationBaseLinetest:durationBaseLinetest, baseline_data:baseline_data, customDate2:customDate2, customDate1:customDate1 },
     type:"POST",
     url:"testing.php",
     // dataType:"html",
     dataType:"JSON",
     success: function(data){
         $('#names').text(data.datatime); 
     }
 });

Here is how todo it, add every single row as an array, so you have an array of arrays. 这是执行操作的方法,将每一行添加为一个数组,这样就拥有一个数组数组。 Also i would suggest you switch to mysqli library instead of mysql because of security reasons and the fact it is not beeing maintained. 我也建议您出于安全原因以及不维护它而切换到mysqli库而不是mysql。

mysql_query($sql1);
$data = [];
while($row = mysql_fetch_array($data11)){
   $data[] = $row;
}
echo json_encode($data);

Use the below Code: 使用以下代码:

$data11 = mysql_query($sql1);
$data = [];
while($row = mysql_fetch_array($data11)) {  
  $data[]["names"] = $row["name"];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM