1

In my database I have user: 1234 with highscore: 222

So I was thinking of getting his data like this (I only have to check for 1 user and I need just the highscore as result it exists):

$highscore =
    mysql_query("SELECT highscore FROM mydatabase WHERE userID = 1234");

Now it can happen that this user is deleted, so I want to do a check using num_rows. If the user doesn't exists anymore, set $highscore = 1; (cause we need this variable somewhere else):

if (mysql_num_rows($highscore) == 0) {
    $highscore = 1;
}

And now echo the result like:

echo '<div id="uhs">'. $highscore .'</div>';

The result is however: Resource id #10

Did some research of course and I think the problem lies in my first query, but I can't seem to fix it...

How to get the 222 echoed?

gioele
  • 9,748
  • 5
  • 55
  • 80
Maurice
  • 1,147
  • 2
  • 21
  • 49

1 Answers1

7

You are trying to print out the resource ID of the query you just ran. To get to the actual results you have to specifically request it:

$result = mysql_query("SELECT highscore FROM mydatabase WHERE userID = 1234");
if (mysql_num_rows($result)) {
    $score = mysql_fetch_assoc($result);
    echo $score['highscore'];
}
else {
    echo 1;
}
John Conde
  • 217,595
  • 99
  • 455
  • 496