Forum Moderators: open

Message Too Old, No Replies

Getting the MySQL Row number of selected data

         

dgsk387

12:00 am on Sep 15, 2007 (gmt 0)

10+ Year Member



I'm using PHP to select some data from a MySQL database.

For example:

SELECT address FROM database WHERE name='daniel'

Name is a unique field so it'll only return 1 match. Once it finds the row where name=daniel I want to be able to tell what row number in the DB that is. How can I do this?

And then afterwards, how can I select data from a specific row (like if I wanted to select the address for the next row)?

Thanks!

phranque

2:11 am on Sep 15, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld, dgsk387!

there is really no concept of a "row number" in a relational db, except in the results set of a given select.
there might be a unique index that autoincrements.
you can add that to your select statement...

SELECT unique_id, address FROM database WHERE name='daniel'

And then afterwards, how can I select data from a specific row (like if I wanted to select the address for the next row)?

"next row" depends on "ORDER BY" clause among other things.
you can use program logic to track which "row number" of a particular results set you are using.

dgsk387

6:20 am on Sep 15, 2007 (gmt 0)

10+ Year Member



Thanks for the welcome phranque!

And thanks for the solution! It's always the straightforward, simple solutions I overlook! This should work out well.

Many thanks.