如何将'id'从mysql查询绑定到$ _SESSION

时间:2012-06-25 18:52:07

标签: mysql session pdo

我试图在mysql查询中将$ _SESSION设置为'id'。我该怎么做?

public function Login($email, $pass){
        if(!empty($email) && !empty($pass)){
            $st = $this->db->prepare("select email, pass, id from login where email=? and pass=?");
            $st->bindParam(1, $email);
            $st->bindParam(2, $pass);
            $st->execute();

            if ($st->rowCount() == 1){
                //login correctly
                $_SESSIONS['user_id'] = XXX;
                header('Location: main.php');
            } else{

            echo "Wrong password";
            }
        } else {
            echo "Please enter username and password";
        }
    }

1 个答案:

答案 0 :(得分:1)

这是我的答案:

public function Login($email, $pass){       
    if(!empty($email) && !empty($pass)){
        $st = $this->db->prepare("select email, pass from login where email=? and pass=?");
        $st->bindParam(1, $email);
        $st->bindParam(2, $pass);
        $st->execute();

        if ($st->rowCount() == 1){
            //login correctly
            $_SESSIONS['user_id'] = $st->fetchALL(); 
            header('Location: main.php');
        } else{
            echo "Wrong password";
        }
    } else {
        echo "Please enter username and password";
    }
}