2

Hi im trying to populate an array with indexes like 'user1' and 'score1' to return to a function.

My Code: It comes up with unindentified variable errors

$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 Answers 2

2

You should use numeric values when you want to use them.. You used a string representation of zero ('0')

$count = 0;
$highscore = array();
while($row = mysql_fetch_array($result))
{       
    $count++; 
    $highscore['user' . $count] = $row['username'];
    $highscore['score' . $count] = $row['score'];
}
Sign up to request clarification or add additional context in comments.

2 Comments

Won't that give an array of arrays?
@gar - true true - my keyboard got away from me for a second there :P
0

If you wanted 'high score' you might want to use max and just return 1 row.

2 Comments

What if it is the users own personal high score?
@lix the query could easily be altered 'SELECT max(score) FROM users WHERE username = 'lix' but he has said that he requires it for a function, presumably to populate a table.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.