登录:
$_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。
答案 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);
还有其他方法可以做到,但这会显示您的一般错误发生的位置。