我正在尝试将结果数组从Mysql转换为JSON格式,该格式似乎不正确,每个对象之间缺少逗号。请指教,谢谢。

我知道我在这里使用了不推荐使用的php版本。

$result = mysql_query("SELECT * FROM patientvaccinedetail")or
die(mysql_error());

while($row = mysql_fetch_array( $result,MYSQL_ASSOC)) {


 $specific = ["message" => $row["message"],
              "mobile" => $row["mobile"]];

             print_r (json_encode($specific));
}


当前结果:

{"message":"hello","mobile":"12345678"}{"message":"hi","mobile":"87878965"}


预期结果:

{"message":"hello","mobile":"12345678"}, {"message":"hi","mobile":"87878965"}

最佳答案

您必须使用数组,并且在循环结束时必须回显结果

$specific = array();
while($row = mysql_fetch_array( $result,MYSQL_ASSOC)) {
     $specific[] = ["message" => $row["message"],
                  "mobile" => $row["mobile"]];
}
echo json_encode($specific);

07-28 02:48
查看更多