Ask the Expert

Adding row numbers to PHP code

Is there any facility to number the output when retrieving data from a database using PHP? For example: 1.output1, 2.output2, and 3.output3

    Requires Free Membership to View

While you could, perhaps, develop a way to generate a row number in a query, it should not be necessary to have a row number at query time. Many users have row ID values stored in their tables to assist with joins and where clauses. Others add row numbers at display time to help users distinguish rows, but PHP should not require row numbers to properly process data.

In any case, as MySQL does not have a row number meta-column, it would be better to add a row number in your PHP code while iterating rows by way of a counter or similar method.

This was first published in January 2006

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: