Access data stored in the result returned from this query

不问归期 提交于 2019-12-13 03:55:08

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/5102455/access-data-stored-in-the-result-returned-from-this-query

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!