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

How to read next row in a mySql Query

P: 2
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.  
Any help would be appreciated
Thank You
May 15 '07 #1
Share this Question
Share on Google+
4 Replies


pbmods
Expert 5K+
P: 5,821
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:
http://www.thescripts.com/forum/thread632487.html
May 15 '07 #2

P: 2
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
May 15 '07 #3

pbmods
Expert 5K+
P: 5,821
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.
May 15 '07 #4

shidec
P: 26
it can be tricked like this :)
[PHP]
$current_row=mysql_fetch_array($result); //read $current_row
while ($next_row = mysql_fetch_array($result)) { //read $next_row
......
if ($current_row['id']==$next_row['id']) { //compare it
.....
.....
}
$current_row=$next_row; //$next_row become current_row on next step :p
}
[/PHP]
May 18 '07 #5

Post your reply

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