我有以下代码段。当我检查结果数组有多少行时,它会打印出1。但是,当我尝试访问其中的数据(名字,姓氏,用户名等)时,它什么也不会打印。
$username = mysqli_real_escape_string($db, $_POST['username']);
$password = mysqli_real_escape_string($db, $_POST['password']);
$password = md5($password);
$query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$results = mysqli_query($db, $query);
if (mysqli_num_rows($results) == 1) {
$session_user = mysqli_fetch_assoc($result);
$_SESSION['firstname'] = $session_user['firstname'];
$_SESSION['lastname'] = $session_user['lastname'];
echo $_SESSION['firstname']; // doesn't print anything
$_SESSION['username'] = $username;
$_SESSION['success'] = true;
header('location: index.php');
}
然而,MYSQL数据库显示数据在那里,并且列名的确正确,顺序为:ID,用户名,名字,姓氏,电子邮件,密码。
我迷路了,我在这里做什么错了?
答案 0 :(得分:0)
您在$result
函数中调用了错误的变量$results
而不是mysqli_fetch_assoc
。
答案 1 :(得分:-2)
也许可以。
if (mysqli_num_rows($results) > 0) {
$session_user = mysqli_fetch_assoc($result);
$_SESSION['firstname'] = $session_user['firstname'];
$_SESSION['lastname'] = $session_user['lastname'];
echo $_SESSION['firstname']; // doesn't print anything
$_SESSION['username'] = $username;
$_SESSION['success'] = true;
header('location: index.php');
}