Jump to content

Archived

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

bonzie

problem with PHP-MySQL

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

Share this post


Link to post
Share on other sites
[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]

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites

×

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.