1

I have a list of players. The players are sorted by points. What I'd like to know is how do I get the ranking number of a CERTAIN player?

This is my code so far (which doesn't work because it seems to have some bugs):

$rank = mysql_query (SET @rank := 0;
SELECT *, @rank := @rank + 1
FROM ava_users
WHERE id = '".$id."'
ORDER BY points DESC);

$rank_res = mysql_fetch_array($rank);

When I try to use my query I get an error message:

mysql_fetch_array() expects parameter 1 to be resource, boolean given in     /Users/***/Documents/Arcades/Arc_development/arc_projects/***/arc_dev_website/arc_offline/includes/profile/profile_main.inc.php
halfer
  • 19,471
  • 17
  • 87
  • 173
drpelz
  • 791
  • 8
  • 41
  • possible duplicate of [mysql\_fetch\_array() expects parameter 1 to be resource, boolean given in select](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-boolean-given-in-select) – putvande Aug 18 '13 at 19:46

3 Answers3

2
$rank = mysql_query (
    "SELECT a.*, 
            (
            SELECT  COUNT(1)
            FROM    ava_users b
            WHERE   (b.points, b.id) >= (a.points, a.id)
            ) AS rank
    FROM    ava_users a
    WHERE   a.`user` = '$id'"
);
federico-t
  • 11,595
  • 17
  • 64
  • 109
1

user is a key word, therefore use user in order to check parameters equality. Also, mysql_query can only execute 1 query at a time.

Orel Eraki
  • 11,436
  • 3
  • 27
  • 34
1

Try this:

SELECT `user`, rank
FROM (
  SELECT `user`, ( @rank := @rank + 1 ) as rank
  FROM ava_users, ( select (@rank := 0 ) ) rnk
  ORDER BY points DESC
) ranks
WHERE `user` = '".$id."'
krokodilko
  • 33,964
  • 6
  • 47
  • 74