MYSQL查询返回'resource id#12而不是它应该返回的数值

时间:2014-01-14 02:26:28

标签: php mysql sql

不确定原因,但这会返回错误的值。我得到了这个资源ID#12,而不是数字值' 1'我正在寻找..

执行此操作的代码是:

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

现在它只给了我一个单词数组。我完全迷失了。帮助?!

编辑::

目前使用的代码是:

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

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

(这应该在导航栏中显示' Usertype为1'

2 个答案:

答案 0 :(得分:1)

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

$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'];

Please, don't use mysql_* functions in new code。它们不再被维护and are officially deprecated。请参阅red box?转而了解prepared statements,并使用PDOMySQLi - this article将帮助您确定哪个。如果您选择PDO here is a good tutorial

答案 1 :(得分:0)

您是否可以更直接地访问您的数据库,例如phpmyadmin?如果是这样,那么在那里运行你的查询,当然是文字表达式:

SELECT account_type from user_attribs WHERE username='lmfsthefounder'
你回来了什么?