1

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)

3 Answers3

1

I believe you're using binding syntax, which is what mysqli uses. PDO just uses placeholders so :consumer_id should be ?.

chris85
  • 23,846
  • 7
  • 34
  • 51
1

If you are using positional placeholders (e.g. ?) Then you can use a normal array like you do now, but if you use named placeholders (e.g. :consumer_id) you have to use an associative array like this:

$q->execute(array(":consumer_id" => $id));

Otherwise it won't know which placeholder has to be replaced with which value.

Rizier123
  • 58,877
  • 16
  • 101
  • 156
0

Please let me know how to debug this

Use $q->debugDumpParams();

Then run the query directly (Mysql termina / Mysql Workbench / phpmyAdmin or however you want) and see if you get the same result as your code

NOTE: I am just answer how I usually test my code with some issue with PDO. This is not a solution to your issue specifically.

Leandro Papasidero
  • 3,728
  • 1
  • 18
  • 33