我有以下查询,该查询在查询数据库时有效:
EDIT 3:根据 Joe Swindell 的建议,可能是查询中的 SET @runningTotal := 0 行。要解决的问题是找到一种方法将下面的复杂查询转换为 JSON 字符串,该字符串也将保存 NumPosts 部分。 NumPosts 是一个运行总计,取决于 SET 行;删除 set 行会删除 mysqli_fetch_array 错误,但会导致所有 NumPost 值均为 NULL。
EDIT 2:我已经回显了下面的查询并粘贴回 MySQL 以确认它正在工作,并且它实际上是有效的。
SET @runningTotal := 0;
SELECT T.ForDate, (@runningTotal := @runningTotal + T.DayProgress) as NumPosts
FROM
(
SELECT DATE( completed_courses.complete_date ) AS ForDate, COUNT( * ) AS DayProgress
FROM users
INNER JOIN completed_courses ON users.user_id = completed_courses.user_id
INNER JOIN wg_courses ON wg_courses.c_id = completed_courses.c_id
INNER JOIN workgroup ON wg_courses.wg_id = workgroup.g_id
WHERE users.course_group LIKE '4h%' AND completed_courses.complete_date > 0
GROUP BY DATE(completed_courses.complete_date)
) T
ORDER BY T.ForDate
以上输出:
ForDate NumPosts
2014-07-29 950
2014-07-30 3063
2014-07-31 3669
2014-08-01 4584
2014-08-02 5088
...
EDIT:检查连接并确保一切匹配,这
输出为:“警告:mysqli_fetch_array() 期望参数 1 为 mysqli_result,给定对象”
PHP代码:
$data = array();
$i = 0;
while($row = mysqli_fetch_array($con, $result)) {
$i += 1;
array_push($data, array($i) + $row);
}
随后,代码被编码为 JSON,以便在 Google Charts 中使用:
var data = <?php echo json_encode($data) ?>
此外,将查询结果导出为 SQL、CSV、Excel 文件等会导致所有 NumPosts 值为 NULL:
ForDate NumPosts
2014-07-29 NULL
2014-07-30 NULL
2014-07-31 NULL
2014-08-01 NULL
2014-08-02 NULL
...
这些值无法正确保存或获取的原因可能是什么?