0

I have the following query:

$sql = "SET @rownum := 0;
              SELECT * FROM (
                SELECT @rownum := @rownum + 1 AS rank, totalpoints, useridFB, username
                FROM user_test ORDER BY totalpoints DESC
                ) as result WHERE useridFB=".$uid."";   

With it I'm getting the rank of a user that has earned points by playing some games in my web.

The query works great when I paste it in phpMyAdmin, I get the correct rank for the user.

What's the problem then? Well, I cannot display any data stored in the result.

I've tried the usual:

$result = mysql_query($sql); 
while ($row = mysql_fetch_assoc($result)) {
    echo $row['totalpoints'];
    echo $row['rank'];
    echo $row['useridFB'];
    echo $row['username'];
} 

But it returns an error:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in ...

An no results printed.

What am I doing wrong? Thanks a lot!

ADM
  • 1,590
  • 11
  • 36
  • 54
  • possible duplicate of [supplied argument is not a valid MySQL result](http://stackoverflow.com/questions/5930594/supplied-argument-is-not-a-valid-mysql-result) – Chris Baker Aug 02 '12 at 05:45

1 Answers1

2

You're not checking the return value of mysql_query(). Most likely, the problem is that you cannot normally run multiple queries via mysql_query() - "SET @rownum := 0;" being the first query, but a quick check with mysql_error() after the failed mysql_query() would be a good idea.

TML
  • 12,813
  • 3
  • 38
  • 45
  • You're probaly right (the connection to the database is correct, I've tried simple queries). Now I'm trying the following but still don't know how to display the data. Thanks again – ADM Feb 24 '11 at 09:15
  • $sql1 = "SET @rownum := 0"; $sql2 = "SELECT * FROM ( SELECT @rownum := @rownum + 1 AS rank, totalpoints, useridFB, usernombre FROM user_test ORDER BY totalpoints DESC ) as result WHERE useridFB=$uid"; mysql_query($sql1); $result = mysql_query($sql2); – ADM Feb 24 '11 at 09:16