1

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

Community
  • 1
  • 1

1 Answers1

1
$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.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345