Is there any wrong with my Code? I tried to display the result but return zero and instead title above occur or is there any way around on how to get this smoothly. Please let me know. Thanks
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION, 1);
$sql = (" SET @curRank1 = 0;
SET @curRank2 = 0;
SELECT
D1.reading_date
,D1.consumer_id
,D1.readings AS previous
,D2.readings AS current
,(D2.readings - D1.readings) AS kwh_used
FROM
(
SELECT M1.*, @curRank1 := @curRank1 + 1 AS rank
FROM consumption M1
LEFT JOIN consumption M2
ON Date(M1.reading_date) = Date(M2.reading_date)
AND M1.reading_date > M2.reading_date
WHERE M2.reading_date IS NULL
) D2
LEFT JOIN
(
SELECT M1.*, @curRank2 := @curRank2 + 1 AS rank
FROM consumption M1
LEFT JOIN consumption M2
ON Date(M1.reading_date) = Date(M2.reading_date)
AND M1.reading_date > M2.reading_date
WHERE M2.reading_date IS NULL
) D1
ON D2.rank = (D1.rank + 1)
WHERE D1.reading_date IS NOT NULL
AND D1.consumer_id = :consumer_id");
$q = $pdo->prepare($sql);
$q->execute(array($id));
$q->nextRowset();
$data = $q->fetchAll(PDO::FETCH_ASSOC);
EDIT: SQLSTATE[HY000]: General error onPDOStatement->fetchAll(2)