我很难将这个PHP解析为JSON kan有人帮我解决了这个如何将这个PHP转换为JSON
$query = "SELECT SUM(total) FROM account";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result))
{
echo $row['SUM(total)'];
}
答案 0 :(得分:1)
尝试json_encode
喜欢
$query = "SELECT SUM(total) as total FROM account";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result))
{
$new_arr[] = $row['total'];
}
echo json_encode($new_arr);
尽量避免使用mysql_*
函数,因为它们已被弃用。请使用mysqli_*
函数或PDO
语句。
答案 1 :(得分:1)
$query = "SELECT SUM(total) as t FROM account";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result))
{
$new_arr[] = $row['t'];
}
echo json_encode($new_arr);
答案 2 :(得分:0)
在php中使用json_encode()
函数。
检查参考here
while($row = mysql_fetch_assoc($result))
{
$myArray[] = $row['SUM(total)'];
}
echo json_encode($myArray);
答案 3 :(得分:0)
while($row = mysql_fetch_assoc($result))
{
$new_arr[] = array("total"=>$row['SUM(total)']);
}
echo json_encode($new_arr);