当我想用 php 和 mysql 回显变量时的资源 ID #10 [英] Resource id #10 when I want to echo a variable with php and mysql

查看:27
本文介绍了当我想用 php 和 mysql 回显变量时的资源 ID #10的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的数据库中,我有用户:1234,高分:222

所以我想像这样获取他的数据(我只需要检查 1 个用户,我只需要它存在的高分):

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

现在这个用户可能会被删除,所以我想用 num_rows 做一个检查.如果用户不再存在,设置 $highscore = 1;(因为我们在其他地方需要这个变量):

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

现在回显结果如下:

echo '

'.$highscore .'</div>';

结果是:Resource id #10

当然做了一些研究,我认为问题出在我的第一个查询中,但我似乎无法解决它...

如何让 222 回显?

解决方案

您正试图打印出您刚刚运行的查询的资源 ID.要获得实际结果,您必须特别要求它:

$result = mysql_query("SELECT highscore FROM mydatabase WHERE userID = 1234");如果(mysql_num_rows($result)){$score = mysql_fetch_assoc($result);回声 $score['highscore'];}别的 {回声 1;}

In my database I have user: 1234 with highscore: 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):

$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. If the user doesn't exists anymore, set $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

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?

解决方案

You are trying to print out the resource ID of the query you just ran. 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;
}

这篇关于当我想用 php 和 mysql 回显变量时的资源 ID #10的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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