当我想用php和mysql回显一个变量时,资源ID#10

时间:2012-05-26 16:11:38

标签: php mysql

在我的数据库中,我有用户:1234 with highscore:222

所以我想要得到这样的数据(我只需要检查1个用户,我只需要得到高分,因为它存在):

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

现在可能会删除此用户,因此我想使用num_rows进行检查。如果用户不再存在,请设置$highscore = 1;(因为我们需要在其他地方使用此变量):

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

现在回复结果如下:

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

但结果是:Resource id #10

当然做了一些研究,我认为问题出在我的第一个问题上,但我似乎无法修复它......

如何让222回应?

1 个答案:

答案 0 :(得分:7)

您正在尝试打印刚刚运行的查询的资源ID。要获得实际结果,您必须明确要求:

$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;
}