访问存储在此查询返回的结果中的数据 [英] Access data stored in the result returned from this query

查看:54
本文介绍了访问存储在此查询返回的结果中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下查询:

$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.

当我将查询粘贴到 phpMyAdmin 时,查询效果很好,我得到了用户的正确排名.

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.

我已经试过了:

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

但它返回一个错误:

警告:mysql_fetch_assoc():提供的参数不是有效的 MySQL 结果资源...

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

没有打印结果.

我做错了什么?非常感谢!

What am I doing wrong? Thanks a lot!

推荐答案

您没有检查 mysql_query() 的返回值.最有可能的问题是您通常无法通过 mysql_query() - "SET @rownum := 0;" 运行多个查询作为第一个查询,但在 mysql_query() 失败后快速检查 mysql_error() 将是一个好主意.

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.

这篇关于访问存储在此查询返回的结果中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆