试图显示用户ID - 显示为0

时间:2013-11-08 16:15:55

标签: php mysql

登录:

$_SESSION['username'] = $username;
$id = mysql_query("SELECT  `id`  FROM  `users` WHERE `username`='" . $username . "'");
$_SESSION['logged_in'] = 1;
$_SESSION['id'] = $id;

索引(试图在此处显示)

Welcome, <?= $username ?><br> ((works))
ID: <?= $id ?>

当实际上它是1时,id只显示为0。

1 个答案:

答案 0 :(得分:2)

mysql_query()返回资源ID,而不是查询结果。您需要使用mysql_fetch_assoc()mysql_result()来获取您的价值:

$res = mysql_query("SELECT  `id`  FROM  `users` WHERE `username`='" . $username . "'");
$result = mysql_fetch_assoc($res);
$id = $result['id'];

$res = mysql_query("SELECT  `id`  FROM  `users` WHERE `username`='" . $username . "'");
$id = mysql_result($result, 0);

还有其他方法可以做到,但这会显示您的一般错误发生的位置。