MYSQL查询传回'resource id#12,而不是传回的数值 [英] MYSQL query returning 'resource id#12 instead of the numerical value it should return

查看:48
本文介绍了MYSQL查询传回'resource id#12,而不是传回的数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知道为什么,但是这返回了错误的值.我正在找回这个资源ID#12,而不是我正在寻找的Number值'1'.

Not sure why, but this is returning the wrong value. I WAS getting this resource ID #12 back, instead of the Number value '1' which I am seeking..

做到这一点的代码是:

$type = "SELECT account_type from user_attribs WHERE username = '$username'";
$usertype= mysql_query($type);

所以我将其更改为:

$type = "SELECT account_type from user_attribs WHERE username = '$username'";
$type_again = mysql_query($type);
$usertype = mysql_fetch_row($type_again);

现在,它给了我单词数组.我对此完全迷失了.帮助吗?!

Now it just gives me the word array. I am completely lost on this. Help?!

:

当前使用的代码是:

  $username= 'lmfsthefounder';
  $type = "SELECT account_type from user_attribs WHERE username='lmfsthefounder';";
  $type_again = mysql_query($type);
  $row = mysql_fetch_row($type_again);
  $usertype = $row['account_type'];
  echo $usertype;

返回如下:主页登录注册用户类型为

which returns like this: Home Login Register Usertype is

(这应该在我的导航栏中显示用户类型为1")

(This should display 'Usertype is 1' in my navigation bar)

推荐答案

您快到了.您有包含MySQL结果的行,这是mysql_fetch_row()返回的结果.将其更改为 mysql_fetch_assoc() ,这将使您的代码成为可能更具可读性.然后,您只需要使用其名称作为数组键来查找要访问的特定列值即可:

You're almost there. You have the row containing the MySQL results which is what mysql_fetch_row() returns. Change that to mysql_fetch_assoc() which makes your code more readable. Then you just need the specific column value you seek which you van access by using it's name as the array key:

$type = "SELECT account_type from user_attribs WHERE username = '$username'";
$type_again = mysql_query($type);
$row = mysql_fetch_assoc($type_again);
echo $row['account_type'];

请不要在新代码中使用mysql_*函数.它们已不再维护并已正式弃用.看到红色框?了解有关 准备好的语句 的信息,并使用 MySQLi -本文将帮助您确定哪一个.如果您选择PDO,这里是一个很好的教程.

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

这篇关于MYSQL查询传回'resource id#12,而不是传回的数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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