使用PHP从db表中获取的JSON消息的格式正在失真
我有一个MySQL数据库表,STUDENT的格式如下:使用PHP从db表中获取的JSON消息的格式正在失真,php,mysql,json,Php,Mysql,Json,我有一个MySQL数据库表,STUDENT的格式如下: ID NAME PLACE MARKS 1 John NYC 62/115 2 Rose SF 43/45 3 Alex DC 54/75 . . . . . . . . . . . . . . .
ID NAME PLACE MARKS
1 John NYC 62/115
2 Rose SF 43/45
3 Alex DC 54/75
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
50 Adam NYC 88/96
我试图通过使用php从这个表中获取数据来创建JSON消息。所需的JSON消息应为:
[{"NAME":"John","ID":"1","PLACE":"NYC","MARKS":"62/115","PERCENTAGE":"53.91%"},{"NAME":"Rose","ID":"2","PLACE":"SF","MARKS":"43/45","PERCENTAGE":"95.56%"},{"NAME":"Alex","ID":"3","PLACE":"DC","MARKS":"54/75","PERCENTAGE":"72%"},.........,{"NAME":"Adam","ID":"50","PLACE":"NYC","MARKS":"88/96","PERCENTAGE":"91.67%"}]
我编写了以下代码:
<!DOCTYPE html>
<meta charset="UTF-8">
<head>
<style>
</style>
</head>
<?php
$servername = "test.server.com";
$username = "test_user";
$password = "test_pass";
$dbname = "test_db";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "select NAME, ID, PLACE, MARKS from STUDENT;";
$temp = array();
$result = $conn->query($sql);
while($row=$result->fetch_assoc())
{
$temp[] = $row;
$number = $row["MARKS"];
$pieces = explode("/", $number);
if ($pieces[1] <> 0) {
$per = round((($pieces[0]/$pieces[1])*100),2);
}
else { $per = 'Undefined';}
$temp1= json_encode($temp).($temp[]['percentage']=$per);
}
$testjson= json_encode($temp1);
echo $testjson;
mysqli_close($conn);
?>
上述格式被歪曲了
如何根据即时标记计算百分比,并以所需格式将其包含在JSON消息中?请帮忙
<!DOCTYPE html>
<meta charset="UTF-8">
<head>
<style>
</style>
</head>
<?php
$servername = "test.server.com";
$username = "test_user";
$password = "test_pass";
$dbname = "test_db";
$conn = new mysqli($servername, $username, $password,$dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "select NAME, ID, PLACE, MARKS from STUDENT;";
$temp = array();
$result = $conn->query($sql);
while($row=$result->fetch_assoc())
{
$number = $row["MARKS"];
$pieces = explode("/", $number);
if ($pieces[1] <> 0) {
$per = round((($pieces[0]/$pieces[1])*100),2);
}else { $per = 'Undefined';}
$row['percentage'] = $per; // add percentage to row
$temp[] =$row; // add row with percentage to temp array
}
$testjson= json_encode($temp1); // note that we only run json_encode once
echo $testjson;
mysqli_close($conn);
?>
请尝试上面的代码。您要调用
json\u encode()
两次,不要这样做。在循环中构造要编码的对象,然后对其进行编码。
<!DOCTYPE html>
<meta charset="UTF-8">
<head>
<style>
</style>
</head>
<?php
$servername = "test.server.com";
$username = "test_user";
$password = "test_pass";
$dbname = "test_db";
$conn = new mysqli($servername, $username, $password,$dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "select NAME, ID, PLACE, MARKS from STUDENT;";
$temp = array();
$result = $conn->query($sql);
while($row=$result->fetch_assoc())
{
$number = $row["MARKS"];
$pieces = explode("/", $number);
if ($pieces[1] <> 0) {
$per = round((($pieces[0]/$pieces[1])*100),2);
}else { $per = 'Undefined';}
$row['percentage'] = $per; // add percentage to row
$temp[] =$row; // add row with percentage to temp array
}
$testjson= json_encode($temp1); // note that we only run json_encode once
echo $testjson;
mysqli_close($conn);
?>