如何从选择查询向会话分配数据

时间:2019-02-01 06:27:51

标签: php session

这是我的代码

if (count($errors) == 0) {
        $password = md5($password);
        $query = "SELECT * FROM admin WHERE username='".$_POST['username']."' AND passcode='".$_POST['password']."'";
        $results = mysqli_query($db, $query);

        if (mysqli_num_rows($results) >0) {
            $_SESSION['username'] = $username;
            $_SESSION['username2'] = "gfgf";
            $_SESSION['username3'] = $row['column_name'];
            $_SESSION['success'] = "You are now logged in";
            header('location: index.php');
        }else {
            array_push($errors, "Wrong username/password combination");
        }
    } 

我想使用$_SESSION['username3'],但是不起作用。我的代码有什么问题。 $_SESSION['username2']很好。 该表中也有匹配的数据。

1 个答案:

答案 0 :(得分:1)

尝试此代码。您错过了此行while ($row = $results->fetch_assoc())$password = md5($_POST['password']);

if (count($errors) == 0) {
    $password = md5($_POST['password']);
    $query = "SELECT * FROM admin WHERE username='".$_POST['username']."' AND passcode='".$_POST['password']."'";
    $results = mysqli_query($db, $query);

    if (mysqli_num_rows($results) >0) {
        while ($row = $results->fetch_assoc()){
            $_SESSION['username'] = $username;
            $_SESSION['username2'] = "gfgf";
            $_SESSION['username3'] = $row['column_name'];
            $_SESSION['success'] = "You are now logged in";
            header('location: index.php');
        }
    }else {
        array_push($errors, "Wrong username/password combination");
    }
}