By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,731 Members | 1,104 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,731 IT Pros & Developers. It's quick & easy.

How to read next row in a mySql Query

P: 2
Hi!

I have this php code which looks like this:
Expand|Select|Wrap|Line Numbers
  1. $sql = ......
  2. $result = mysql_query($sql,$connection)
  3. or die("Couldn't execute SELECT query");
  4.  
  5. while ($row = mysql_fetch_array($result)) {
  6. ......
  7. }
  8.  
What i want to do is be able to read the next row in the query. So for example:
Expand|Select|Wrap|Line Numbers
  1. while ($row = mysql_fetch_array($result)) {
  2. ......
  3. if (ID for next row/record = ID for this row/record) {
  4. do this
  5. }
  6. }
  7.  
I almost got the solution , but the very last record for my query is always missing using this code:
Expand|Select|Wrap|Line Numbers
  1. $current_row=mysql_fetch_array($result); //read $current_row
  2. while ($next_row = mysql_fetch_array($result)) { //read $next_row
  3. ......
  4. if ($current_row['id']==$next_row['id']) { //compare it
  5. .....
  6. .....
  7. }
  8. $current_row=$next_row; //$next_row become current_row on next step
  9. }
  10.  
Thanks for any help
Jan 12 '12 #1
Share this Question
Share on Google+
2 Replies


Rabbit
Expert Mod 10K+
P: 12,364
And it should be missing. The last row has no next row to compare to.
Jan 12 '12 #2

P: 2
The user mikosiko (http://forums.devnetwork.net) gives me the perfect solution for this problem:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.   // Example
  4.   $heading_column = '<whatever is the name of your heading column>';
  5.   $last_heading = null;
  6.  
  7.   while($row = your_fetch_assoc_statement){
  8.         // detect a change in the heading value and output the new heading
  9.         if($last_heading != $row[$heading_column]){
  10.                 // detect if it is not the first heading - close out the previous section
  11.                 if($last_heading != null){
  12.                         // your code to close the previous section (table, div, etc)...
  13.                         echo "close section<br />";
  14.                 }
  15.                 // output the new heading here...
  16.                 echo "new section title - {$row[$heading_column]}<br />";
  17.  
  18.                 // save the new heading as the last_heading
  19.                 $last_heading = $row[$heading_column];
  20.         }
  21.         // output the actual data here...
  22.         echo "data - {$row['your_data']}<br />";
  23.   }
  24.   // if there was any output - close out the last section
  25.   if($last_heading != null){
  26.         // your code to close the previous section (table, div, etc)...
  27.         echo "close section<br ?>";
  28.   }
  29. ?>
  30.  
Jan 13 '12 #3

Post your reply

Sign in to post your reply or Sign up for a free account.