if (isset($_POST["username"]) && isset($_POST["password"])) {
$adminuser = $_POST["username"];
$password = $_POST["password"];
// Connecting to database
include "../StorageScript/connecttomysql.php";
//sql query
$sql = mysql_query("SELECT id FROM admin WHERE username='$adminuser' AND password= '$password' LIMIT 1");
$existCount = mysql_num_rows($sql);
while ($row = mysql_fetch_array($sql)) {
$id = $row["id"];
}
$_SESSION['id'] = $id;
$_SESSION['adminuser'] = $adminuser;
$_SESSION['password'] = $password;
}
在上面的代码中,我试图从数据库中获取用户ID并将其放入会话变量中。当我尝试在另一个页面中回显$ _SESSION [' id']时,它不会显示任何内容。但是,当我尝试回应用户名时,它会起作用。
答案 0 :(得分:0)
如果您使用此命令mysql_fetch_array($sql)
,请使用$row[0]
。
while ($row = mysql_fetch_array($sql)) {
$id = $row[0];
}
或者如果您想使用$row["id"]
,请使用mysql_fetch_assoc($sql)
while ($row = mysql_fetch_assoc($sql)) {
$id = $row["id"];
}
当然你应该使用mysqli_ *或PDO而不是弃用的mysql_ *函数。
答案 1 :(得分:0)
您只需要查询中的一行.. 所以不要使用while循环....
if (isset($_POST["username"]) && isset($_POST["password"])) {
$adminuser = $_POST["username"];
$password = $_POST["password"];
// Connecting to database
include "../StorageScript/connecttomysql.php";
//sql query
$sql = mysql_query("SELECT id FROM admin WHERE username='$adminuser' AND password= '$password' LIMIT 1");
$existCount = mysql_num_rows($sql);
$row = mysql_fetch_array($sql);
$id = $row['id'];
$_SESSION['id'] = $id;
$_SESSION['adminuser'] = $adminuser;
$_SESSION['password'] = $password;
}