【发布时间】:2018-01-27 01:31:57
【问题描述】:
我有一个 SQL 查询,我使用一个 foreach 将这些值返回到一个 JSON 数组中。这是我的代码的样子:
$sql = "SELECT SUM(sales) from sales ... blah blah";
$result = mysqli_query($connection, $sql);
$output = array();
foreach(result as $row) {
$output[] = $row;
}
$json = json_encode($output);
echo $json; // Returns the array
数组当前如下所示:
[{"SUM(sales)}"."10000"},[{"SUM(sales)}"."51221"},[{"SUM(sales)}"."2351"}]
我希望它是这样的:
[10000, 51221, 2351]
当我使用decode($output) 时,它返回NULL。
【问题讨论】:
-
“销售”表是否有“销售”列?
-
(*) 如果您的 sql 有用户输入(例如来自 GET 或 POST 变量),那么您需要更改为使用准备好的语句、执行和 fetch。否则你很容易受到 sql 注入攻击。
-
decode($output)是什么意思?这不是我所知道的任何 php 函数。 -
@derloopkat 是的
-
@IncredibleHat
json_decode($output),我的意思是。对不起!