2012-05-23 58 views
2

嗨即時嘗試填充索引像'user1'和'score1'返回到一個函數。從while循環填充數組返回PHP

我的代碼:它與unindentified變量錯誤

出現
$query = 'select * from users order by score desc'; 
$result = mysql_query($query) or die (mysql_error()); 
$highscore = array(); 
$count = '0'; 

while($row = mysql_fetch_array($result)) 
{ 
    $count++;   
    $highscore = array('user' . $count => $row['username'], 'score' . $count => $row['score']); 
} 

return $highscore; 

回答

2

當你想使用它們,您應該使用的數值。您使用的零('0'

$count = 0; 
$highscore = array(); 
while($row = mysql_fetch_array($result)) 
{  
    $count++; 
    $highscore['user' . $count] = $row['username']; 
    $highscore['score' . $count] = $row['score']; 
} 
+0

這不會給數組的數組? – Gareth

+0

@gar - true true - 我的鍵盤在第二秒離開了我:P – Lix

0
字符串表示

如果您想要「高分」,您可能需要使用max並返回1行。

+0

如果是用戶自己的個人高分怎麼辦? – Lix

+0

@lix查詢可以很容易地改變'SELECT max(score)FROM users WHERE username ='lix'但他說他需要它作爲一個函數,大概是爲了填充表格。 – 2012-05-23 22:53:36