1
votes

I have a long query return of which I need to use the current and next row to calculate some results. Can you do this without using fetch all?

I'm not sure how this is duplicate to my question ref to Your Common Sense

My question is PDO not mysql;

Example

    |  co1 1   |  col2   |
    |    1     | 444.44  |
    |    2     | 666.66  |
    | ....     | ......  |
    |  99999   | 111.11  |

IN PDO/PHP

        $query   = $pdo>query(sqlQuery);
        while ($rs = $query->fetch(PDO::FETCH_OBJ)): 
         #NEED HELP HERE
         if($rs->col2 //row2
              > 
            $rs->col2 //row1
           )
          then{
           do someting...
           }


          endwhile;
         }

Hope that gives you an idea of what I want to do

thanks for your help

1
Try Using ORDER BY <primary key, or appropriate column> DESC LIMIT 2 ! FETCH can be used, but it will give you single row, If you still can't do it, Please show your Query ! - Hytool
I have a long query WHERE is the QUERY - Mohit Shrivastava
Yes, dont use fetch all just fetch one row at a time in a loop :) while ($row = $stmt->fetch()) { - Hanky Panky

1 Answers

1
votes
$query   = $pdo>query(sqlQuery);
$current = $query->fetch(PDO::FETCH_OBJ)
while ($next = $query->fetch(PDO::FETCH_OBJ))
{
    // do your calc here
    $current = $next;
}

It's still has not much to do with PDO but with loops in general. PDO doesn't have a method to foresee the next row, if you were asking for this.