0

I am fairly new to programming and got stuck in this infinite while loop when I am trying to show the results of the sql query in an order.

I have the entries ordered by date and time inside mySQL table.

When I display the results it does show up in the right order but I wanted to have a counter and display that result in order 1,2,3..etc.

Right now I am showing the EventIDs in the html table straight from mySQL variable "EVENTID" from table 'eventtable'.

That displays the EVENTIDs which are of course not necessarily in order.

So I tried a while loop but it seems like I can not escape out from the first row of what mysqli_fetch_array() returns.

Any help/suggestions will be greatly appreciated.

1
  • post the code you have tried Commented Feb 6, 2014 at 17:26

1 Answer 1

1

Do you need this counter to be part of the actual query results, or just something you display to the user? user display is easy:

$counter = 1;
while($row = fetch_result_row_using_your_db_library()) {
   echo $counter;
   $counter++;
   ... do other stuff ..
}

Doing it at the DB level is a big uglier, involving DB variables and a self-incrementing counter in the query fields:

SET @row := 0;
SELECT @row := @row + 1 AS currow,  * FROM .... WHERE ... ORDER BY ...
Sign up to request clarification or add additional context in comments.

5 Comments

Just displaying to the user.
Thanks a lot Mark. I put a while loop inside the other while loop of the fetch querry, that's why fell into the infinite while loop! Doh me! Just like my username "smokedMeat"! Cheers!
I did the self increment in the DB level with my primary key to suffice some other purpose but that's not what I was trying to to display to the user. Thanks again Mark!
@Marc B, is there any reason to do not use 'echo $counter++' on a single line?
@vlad: keeping it simple... some people need to be spoon-fed answers, as opposed to being handed a package they need to unwrap themselves.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.