Quantcast
Channel: PHP Freaks: PHP Help
Viewing all articles
Browse latest Browse all 13200

Getting a users rank from a mysql table with PDO

$
0
0

Hi, I am trying to get the rank of a user in a database, based on his score, so when the user logs in he can see his rank. I am using this to build a list of all users ordered by their score for a different section.

$stmt = $dbh->query("SELECT * FROM $table ORDER BY score DESC"); 
 
    $rank = 0;
        while($uid = $stmt->fetchObject()) {
          $rank++;
          echo $uid->uname ."is ranked". $rank;
        };

How can I get ONLY a users rank in the db based on his ID number, if I have a table like, .

 

| user | ID | score |

Jim  | 2568 | 80

Pete | 5693 | 115

Sam | 3258 | 569

George | 7412 | 30

 

the ID numbers are a unique key

 

If Jim logs in today he will see:

 

Your Current Rank is 3

 

I am using PHP with PDO on a MySQL table. Thanks!


Viewing all articles
Browse latest Browse all 13200

Trending Articles