简体   繁体   English

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

[英]Resource id #10 when I want to echo a variable with php and mysql

In my database I have user: 1234 with highscore: 222 在我的数据库中,我的用户:1234,高分:222

So I was thinking of getting his data like this (I only have to check for 1 user and I need just the highscore as result it exists): 因此,我正在考虑以这种方式获取他的数据(我只需要检查1位用户,并且只需要得到高分即可):

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

Now it can happen that this user is deleted, so I want to do a check using num_rows. 现在可能会删除该用户,因此我想使用num_rows进行检查。 If the user doesn't exists anymore, set $highscore = 1; 如果用户不存在,则设置$highscore = 1; (cause we need this variable somewhere else): (因为我们在其他地方需要此变量):

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

And now echo the result like: 现在回显结果,如下所示:

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

The result is however: Resource id #10 结果是: Resource id #10

Did some research of course and I think the problem lies in my first query, but I can't seem to fix it... 当然做了一些研究,我认为问题出在我的第一个查询中,但是我似乎无法解决它。

How to get the 222 echoed? 如何获得222的回音?

You are trying to print out the resource ID of the query you just ran. 您正在尝试打印出刚运行的查询的资源ID。 To get to the actual results you have to specifically request it: 要获得实际结果,您必须特别要求它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM