How to read next row in a mySql Query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • phphelp87
    New Member
    • May 2007
    • 2

    #1

    How to read next row in a mySql Query

    I have this php code which looks like this
    Code:
    $sql = ......
    $result = mysql_query($sql,$connection)
    or die("Couldn't execute SELECT query");
    
    while ($row = mysql_fetch_array($result)) {
    ......
    }
    What i want to do is be able to read the next row in the query. So for example
    Code:
    while ($row = mysql_fetch_array($result)) {
    ......
    if (ID for next row/record = ID for this row/record) {
    do this
    }
    }
    Any help would be appreciated
    Thank You
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, phphelp87. Welcome to TSDN!

    Your best bet would be to compile all your data first, then process it. That way, you have access to all your rows at any given time.

    Check out this article for a fantastic tutorial on setting this up:

    Comment

    • phphelp87
      New Member
      • May 2007
      • 2

      #3
      Thanks for the quick reply
      Is this the only way to read different rows in a sql query?
      If so i will have an attempt

      Thank You

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Originally posted by phphelp87
        Is this the only way to read different rows in a sql query?
        If it isn't the only way, it's generally agreed to be the best way. Plus, it's reusable, so you'd only have to write this code once.

        Comment

        • shidec
          New Member
          • May 2007
          • 26

          #5
          it can be tricked like this :)
          [PHP]
          $current_row=my sql_fetch_array ($result); //read $current_row
          while ($next_row = mysql_fetch_arr ay($result)) { //read $next_row
          ......
          if ($current_row['id']==$next_row['id']) { //compare it
          .....
          .....
          }
          $current_row=$n ext_row; //$next_row become current_row on next step :p
          }
          [/PHP]

          Comment

          Working...