2010-06-04 43 views
0

我有以下代碼我想從MySQL獲得播放速度排名。如何從那裏得到球員排名統計在PHP和MySQL

MySQL表是帳戶

Username | Speed 
----------------- 
Player1  21 
Player2  52 
Player3  33 

Player2 52(Ranked:1) 
Player3 33(Ranked:2) 
Player2 21(Ranked:3) 

$result = mysql_query("SELECT * FROM accounts") or die (mysql_error()); 
while($row = mysql_fetch_array($result)) { 
      $username= $row[username]; 
$speed = $row['speed']; 
    } 

抱歉,我一直在嘗試不同的方式,但我不能讓它的工作

回答

2

爲什麼不乾脆:

$result = mysql_query("SELECT * FROM accounts ORDER BY speed DESC") or die (mysql_error()); 

$rank = 1; 
while($row = mysql_fetch_array($result)) { 
    $username= $row[username]; 
    $speed = $row['speed']; 
    $rank++; 
} 
1

僅僅通過速度對它們進行排序:

$result = mysql_query("SELECT * FROM accounts ORDER BY speed DESC") or die (mysql_error()); 
$prev_rank = 0; 
while($row = mysql_fetch_array($result)) { 
      $username= $row['username']; 
      $speed = $row['speed']; 
      $rank = ++$prev_rank; 
    } 

閱讀ORDER BY

1

使用它:

$result = mysql_query("SELECT * FROM accounts ORDER BY speed DESC") or die (mysql_error());