[英]Convert PHP array from mysql to JSON format
I'm trying to convert the result array from Mysql to JSON format, the format doesn't seems to be correct, a comma is missing between each object. 我正在尝试将结果数组从Mysql转换为JSON格式,该格式似乎不正确,每个对象之间缺少逗号。 Please advice, thank you.
请指教,谢谢。
I'm aware that im using the deprecated version of php here. 我知道我在这里使用了过时的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));
}
Current Result: 当前结果:
{"message":"hello","mobile":"12345678"}{"message":"hi","mobile":"87878965"}
Desired Result: 所需结果:
{"message":"hello","mobile":"12345678"}, {"message":"hi","mobile":"87878965"}
You have to use array and at end of loop you have to echo result 您必须使用数组,并且在循环结束时必须回显结果
$specific = array();
while($row = mysql_fetch_array( $result,MYSQL_ASSOC)) {
$specific[] = ["message" => $row["message"],
"mobile" => $row["mobile"]];
}
echo json_encode($specific);
Please moved json_encode() to outside of while loop. 请将json_encode()移至while循环之外。
$specific = array();
while($row = mysql_fetch_array( $result,MYSQL_ASSOC)) {
$specific = ["message" => $row["message"],
"mobile" => $row["mobile"]];
}
print_r(json_encode($specific));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.