Page 1 of 1

Get the position of a row in a result set?

Posted: Sat Sep 22, 2007 1:01 pm
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?

Posted: Sat Sep 22, 2007 1:14 pm
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++;
}

Posted: Sat Sep 22, 2007 2:18 pm
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. :?

Posted: Sat Sep 22, 2007 3:00 pm
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.