473,387 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to read next row in a mySql Query

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
2 1122
Rabbit
12,516 Expert Mod 8TB
And it should be missing. The last row has no next row to compare to.
Jan 12 '12 #2
Reboot
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

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

Similar topics

3
by: joemyre | last post by:
Hi everyone, What I'm trying to do is take php variables i got from user input, and pass them as the MySQL query terms. $query = "select * from ident where ".$searchtype1."=".$searchterm1."";...
2
by: JDJones | last post by:
Using PHP and MySQL. Trying to put a list of categories into a drop down select option of a form like: <form name="form" action="<? print $_SERVER?>" method="get"> <select name="subject">...
0
by: Murphy | last post by:
I am currently migrating a db from SQL Server & MySQL and ? (front end yet to be decided upon) As I familiarise myself with MySQL populating tables etc can be quite clumbersome. After reading the...
0
by: taras.di | last post by:
Hi everyone, I've come across an extremely strange problem. The exact same query in both mysql command line client, and mysql query browser gives entirely different results. I was hoping someone...
2
by: Flic | last post by:
Hi, I have a basic db that I access with MySQL query browser. Everything seems fine to me but I am using this db as part of a php shopping basket and when I try to add an item I get: Notice:...
2
by: stmfc | last post by:
i have a problem with user creation for mysql db i think the problem stems from string style. GRANT ALL ON newDB.* TO 'test'@'localhost' IDENTIFIED BY 'test'; i am...
4
by: John | last post by:
Hello All, Can someone help me with a problem I am having? I have a form which has a multi-select list where the user can select more than one company name from a list and I need to pass those...
1
by: pretzelboy | last post by:
Hi, I last wrote software 13years ago in the pascal, dbase, clipper days. I have recently built a Ubuntu Box and with C++ (and help from the web) setup a serial barcode reader program using Mysql...
5
by: lisles | last post by:
i have a page funtion.php which hs the function to connect to the db /* Mysql Connection */ function connect(){ global $db_server,$db_user,$db_pass,$db;//Global Values from the config.php...
2
by: digituf | last post by:
i have 2 forms here. 1) DisplayDetails.php 2) RegistrationForm.php when user click to the link 'Next' at the DisplayDetails.php page it will bring all the session value to the...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.