Jump to content


Photo

problem with PHP-MySQL


  • Please log in to reply
5 replies to this topic

#1 bonzie

bonzie
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 19 June 2006 - 04:18 PM

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
$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];

I expected to see all the user_id however I don't get anything at all. Can anyone help me with this?

Thanks

#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 19 June 2006 - 04:40 PM

$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>";
}

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 19 June 2006 - 04:43 PM

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.

<?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>';
?>

Ken

#4 paul2463

paul2463
  • Members
  • PipPipPip
  • Advanced Member
  • 955 posts
  • LocationUK

Posted 19 June 2006 - 04:45 PM


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
you cannot affect the past but you can ruin a perfectly good present by worrying about the future

*********************************************************************
The <A HREF="http://www.php.net/d...php">MANUAL</A> is actually a useful resource

#5 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 19 June 2006 - 04:53 PM

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

#6 bonzie

bonzie
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 19 June 2006 - 06:19 PM

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) View Post[/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) View Post[/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]





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users