Jump to content

problem with PHP-MySQL


bonzie

Recommended Posts

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
Link to comment
https://forums.phpfreaks.com/topic/12388-problem-with-php-mysql/
Share on other sites

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
Link to comment
https://forums.phpfreaks.com/topic/12388-problem-with-php-mysql/#findComment-47348
Share on other sites


Hi

I 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 sorry

Paul
Link to comment
https://forums.phpfreaks.com/topic/12388-problem-with-php-mysql/#findComment-47351
Share on other sites

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]
Link to comment
https://forums.phpfreaks.com/topic/12388-problem-with-php-mysql/#findComment-47390
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.