Get the position of a row in a result set?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
1337D00D
Forum Newbie
Posts: 2
Joined: Sat Sep 22, 2007 12:50 pm

Get the position of a row in a result set?

Post by 1337D00D »

I can't seem to figure out how get the position (1st row in result set, 2nd row, 3rd, etc.) of a row in a result set from a SELECT. (PHP).

Code: Select all

$result1=mysql_query("SELECT * FROM table WHERE name='myname'");

$result2=mysql_query("SELECT * FROM table ORDER BY name DESC");
How would I figure out what place the row $result1 is in the result set $result2 in the simplest possible way?
User avatar
Josh1billion
Forum Contributor
Posts: 316
Joined: Tue Sep 11, 2007 3:25 pm

Post by Josh1billion »

Looking at your code versus what you're saying, it looks like you might be misunderstanding something. The MySQL queries above will give you ALL of the results, not just one. So you could do something like this...

Code: Select all

$results=mysql_query("SELECT * FROM table ORDER BY name DESC");

$i = 1;
while ($current_result = mysql_fetch_array($results))
{ // this loop will get a result and store its data as an array in $current_result
    print "This is result number $i and its name is " . $current_result['name'] . "<BR>";
    $i++;
}
1337D00D
Forum Newbie
Posts: 2
Joined: Sat Sep 22, 2007 12:50 pm

Post by 1337D00D »

I know, I just did that as an example.

In your example you're using the number to get the value of column 'name', I want to use the value of column 'name' to get the number.

"This name is ____ and it is result number $i."

Instead of

"This is result number $i and its name is ____."

Sorry, this is a kinda hard to explain. :?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Just so you know I have no idea what you are asking, so perhaps maybe other people may be confused as well.
Post Reply