How can I find the position a certain row has in my database table?
In my database I have a bunch of Players from a game. They have columns such as ID, name, level, world, and so on.
I want to sort them by Level, and see what position they are in in the list.
So far I've tried this, but it only prints out 0 (the start value I put). It does not seem to iterate through.
Previously I made sure the name is stored in $name
.
$query = "SELECT * FROM rookstayers ORDER BY level DESC";
$globalPos = 0;
$result = mysql_query($query);
$num = mysql_numrows($result);
$i = 0;
while($i < $num) {
$posName = mysql_result($result, $i, 'name');
if($posName == '$name')
{
$globalPos = $i;
break;
}
$i++;
}
If my table looks like this (after sorting it by level):
name - level
Joe - 50
Jacob - 47
Sarah - 34
Anna - 19
Then "Sarah" would be number 3. Anna number 4, etc...
I want the position-number to be in $globalPos
.
I only found pure SQL code for this, but I want it in PHP. Is it not possible to do in PHP?