i have some issue on getting cumulative sum by month with PDO/MySQL, php 7.0 and mysql 5.5.
With multiple queries, i got "Excention : SQLSTATE[HY000]: General errorvar" as error. If i remove those "SET" lines, it disappear but i cannot have what i am trying to do.
$sql = "
SET @accInc := 0;
SET @accOut := 0;
SELECT
q1.y as year,
q1.m as month,
(@accInc := @accInc + q1.income) AS accInc,
(@accOut := @accOut + q1.outgo) AS outInc
FROM
(SELECT
YEAR(`date`) AS y,
MONTH(`date`) AS m,
SUM(CASE WHEN amount > 0 THEN amount ELSE 0 END) as income,
SUM(CASE WHEN amount < 0 THEN amount ELSE 0 END) as outgo
FROM `transaction`
GROUP BY y, m
ORDER BY y, m
) AS q1";
require("./pdo.php");
try {
$req = $db->prepare($sql);
$req->execute();
$data = $req->fetchAll(PDO::FETCH_ASSOC);
foreach($data as $row) {
$acc[$row["year"]][$row["month"]] = array(
"inc" => (float)$row["accInt"],
"out" => (float)$row["accOut"]
);
}
} catch(PDOException $except) {
printf("Excention : %s", $except->getMessage());
}
Thank you.
Edit : I had to set $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
And isolate SET
command. Now it works.