在我的数据库中,我有用户:1234,高分: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得到回声?
您正在尝试打印出刚刚运行的查询的资源 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;
}