I have the following query which is valid when querying the database:
EDIT 3: Per Joe Swindell's advice, it could be that the SET @runningTotal := 0 line in the query. The issue to address is finding a way to convert the complex query below into a JSON string which will also save the the NumPosts part. NumPosts is a running total which is dependent on the SET line; removing the set line removes the mysqli_fetch_array error, but results in NULL for all NumPost values.
EDIT 2: I have echoed the query below and pasted back into MySQL to confirm that it is working, and it is in fact valid.
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
The above outputs:
ForDate NumPosts
2014-07-29 950
2014-07-30 3063
2014-07-31 3669
2014-08-01 4584
2014-08-02 5088
...
EDIT: Checked the connection and made sure everything matches, and this is the output: "Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, object given"
PHP code:
$data = array();
$i = 0;
while($row = mysqli_fetch_array($con, $result)) {
$i += 1;
array_push($data, array($i) + $row);
}
Later, the code is encoded into JSON for use in Google Charts:
var data = <?php echo json_encode($data) ?>
In addition, exporting the query results as SQL, CSV, Excel files, etc. results in all NumPosts values as NULL:
ForDate NumPosts
2014-07-29 NULL
2014-07-30 NULL
2014-07-31 NULL
2014-08-01 NULL
2014-08-02 NULL
...
What could be the reason the values are not saving or fetching properly?
mysql_queryinterface. It's awful and is being removed in future versions of PHP. A modern replacement like PDO is not hard to learn. A guide like PHP The Right Way can help explain best practices. Always be absolutely sure your user parameters are properly escaped or you will have severe SQL injection bugs.