Access data stored in the result returned from thi

2019-09-05 06:14发布

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!

标签: php mysql row
1条回答
Animai°情兽
2楼-- · 2019-09-05 06:24

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.

查看更多
登录 后发表回答