Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get Row Number in MySQLi

Status
Not open for further replies.

PCHomepage

Programmer
Feb 24, 2009
609
US
How can I get the row number in a list of entries? It's not the number of rows in the result that I need: it is a line by line count of each one.
 


MySQL doesn't have 'row numbers' per se, the records returned are simply an array, going from 0 to (count - 1), in whatever sort order was specified in the query.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
That's what I thought. I'm trying to create a function to provide the row number when run inside a loop so how would I do it? I'm not sure where to begin.
 
That's what I thought. I'm trying to create a function to provide the row number when run inside a loop so how would I do it? I'm not sure where to begin.

Can you not just add a counter?


For example:

Code:
$rowNumber = 1;
while($row=mysqli_fetch_row($result))
{
[indent]echo $rowNumber . $row['fieldname'];[/indent]
$rowNumber++;
}

This will increase $rownumber by one each time a row is echoed out.

Or is there something else you are trying to accomplish?



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top