bonzie Posted June 19, 2006 Share Posted June 19, 2006 Hi all,I am a beginner in PHP and therefore I need your help with the following. I have created in MySQL a table 'scores' with columns user_id and score.Now, I want to present the user_ids ordered by their score...I have done the folowing[code]$query="select user_id from scores order by score desc";$result=mysql_query($query);$num_results=mysql_num_rows($result);$row=mysql_fetch_row($result);for ($count=0; $count<$num_results; $count++)echo row[$count];[/code]I expected to see all the user_id however I don't get anything at all. Can anyone help me with this?Thanks Quote Link to comment https://forums.phpfreaks.com/topic/12388-problem-with-php-mysql/ Share on other sites More sharing options...
.josh Posted June 19, 2006 Share Posted June 19, 2006 [code]$query="select user_id from scores order by score desc";$result=mysql_query($query);while ($row = mysql_fetch_array($result)) { echo $row['user_id'] . "<br>";}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/12388-problem-with-php-mysql/#findComment-47347 Share on other sites More sharing options...
kenrbnsn Posted June 19, 2006 Share Posted June 19, 2006 You need to put the fetch within the while loop (or as it's condition), you don't needt the mysql_num_rows() function, you should use the function mysql_fetch_assoc(), and you should output a '<br>' tag with the 'echo'.Other than those issues, you were on the right track.[code]<?php$query="select user_id from scores order by score desc";$result=mysql_query($query);while ($row=mysql_fetch_assoc($result)) echo row['user_id'].'<br>';?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/12388-problem-with-php-mysql/#findComment-47348 Share on other sites More sharing options...
paul2463 Posted June 19, 2006 Share Posted June 19, 2006 HiI am also new to PHP but I think I see a problem with your code from what I do know unless PHP is lax over this. you have put the following:-for ($count=0; $count<$num_results; $count++)echo row[$count];you have braces missing from it and it should look like this:-for ($count=0; $count<$num_results; $count++){echo row[$count];}thats the summ of my knowledge so if it does not help then I am sorryPaul Quote Link to comment https://forums.phpfreaks.com/topic/12388-problem-with-php-mysql/#findComment-47351 Share on other sites More sharing options...
kenrbnsn Posted June 19, 2006 Share Posted June 19, 2006 When there is only one statement in a statement block, you don't need the curly braces. The answer to to the OP's question was answered by the first two responders.Ken Quote Link to comment https://forums.phpfreaks.com/topic/12388-problem-with-php-mysql/#findComment-47353 Share on other sites More sharing options...
bonzie Posted June 19, 2006 Author Share Posted June 19, 2006 Thnx for your help.Some further in my scrpit I use a similar query, however there I only want to show the first 20 rows?How can i implement this?[!--quoteo(post=385684:date=Jun 19 2006, 06:53 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Jun 19 2006, 06:53 PM) [snapback]385684[/snapback][/div][div class=\'quotemain\'][!--quotec--]When there is only one statement in a statement block, you don't need the curly braces. The answer to to the OP's question was answered by the first two responders.Ken[/quote]I solved it myself...[!--quoteo(post=385722:date=Jun 19 2006, 08:15 PM:name=bonzie)--][div class=\'quotetop\']QUOTE(bonzie @ Jun 19 2006, 08:15 PM) [snapback]385722[/snapback][/div][div class=\'quotemain\'][!--quotec--]Thnx for your help.Some further in my scrpit I use a similar query, however there I only want to show the first 20 rows?How can i implement this?[/quote] Quote Link to comment https://forums.phpfreaks.com/topic/12388-problem-with-php-mysql/#findComment-47390 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.