输出球员排名的排名?

时间:2014-09-17 02:30:35

标签: php mysql mysqli

我正在对我的表进行排序,该表具有用户的idhighscore。这是按降序排序的,我正在传递当前用户的id以找到他的排名。

I.e:我传入当前用户的ID,并希望echo排名。

select *
from (
  select *
  from players
  order by highscore desc
  limit 10
) t
where id = $current_user

我已经尝试了这个但是没有运气在PHP中获取数据输出..

$result = $mysqli->query("SELECT COUNT(*) AS rank FROM players WHERE highscore >= (SELECT highscore FROM players WHERE id=$current_user)");

我必须修改以上内容才能获得用户排名,然后echo输出他在PHP中的排名?谢谢!

1 个答案:

答案 0 :(得分:0)

一旦从数据库中查询数据,就需要将数据放入数组中。尝试使用mysqli's fetch_assoc,如下所示:

$result = $mysqli->query("SELECT COUNT(*) AS rank FROM players WHERE highscore >= (SELECT highscore FROM players WHERE id=$current_user)");

// Put the data from the query into an array
$row = $result->fetch_assoc();

// Store the rank in a variable named $rank
$rank = $row['rank'];

echo $rank;