Php 会话脚本未设置变量

Php 会话脚本未设置变量,php,session,pdo,Php,Session,Pdo,我是会话新手,认为我的代码工作不正常 我的目标是使用PDO而不是SQL,但由于我也是PDO新手,我有点卡住了 我正试图将$who设置为一个会话,但当我在SQL和PDO之间切换时,事情变得让我困惑。你能帮我把东西做成犹太洁食吗 <?php require_once('/scripts/include.php'); require_once('/scripts/includePDO.php'); $error = ''; $form = $_POST['submit']; $email =

我是会话新手,认为我的代码工作不正常

我的目标是使用PDO而不是SQL,但由于我也是PDO新手,我有点卡住了

我正试图将$who设置为一个会话,但当我在SQL和PDO之间切换时,事情变得让我困惑。你能帮我把东西做成犹太洁食吗

<?php
require_once('/scripts/include.php');
require_once('/scripts/includePDO.php');

$error = '';
$form = $_POST['submit'];
$email = $_POST['email'];
$password = $_POST['password'];

if( isset($form) ) {
if( isset($email) && isset($password) && $email !== '' && $password !== '' ) {

$sql = "SELECT * FROM tbl_users WHERE email = :email and password = :password";

$q   = $conn->prepare($sql); // the default way of PDO to manage errors is quite the same as `or die()` so no need for that
        $q->bindValue(':email',$email,PDO::PARAM_STR);
        $q->bindValue(':password',$password,PDO::PARAM_STR);
        $q->execute();

            $r = $q->fetch(PDO::FETCH_ASSOC);
            if(($r)!=0)

{ //success

$info = mysql_fetch_array( $sql );
$answer = $info['id'];

$_SESSION['logged-in'] = true;
$_SESSION['who']=$answer;

//If the login details are entered and they match those in the database, forward to new page.
header('Location: /social3/home/');

exit;

// If information is wrong or missing, provide error message.
} else { echo "Sorry, something hasn't worked. Are you entering all the information correctly?"; }
} 

}

?>

您正在使用PDO,因此这些行将无法工作-

$info = mysql_fetch_array( $sql );
$answer = $info['id'];
您已经获取了该行并将其存储在
$r
中,因此只需-

$answer = $r['id'];

回应您的编辑


会话_start()位于包含的php代码/页面顶部是必需的。没有它,您就无法执行
$\u会话['logged-in']=true&
$\u会话['who']=$answer。您需要启动会话才能设置会话值,并且必须在发送任何数据/头之前启动会话。请看一下手册-

实际上,当您在这里时,如果您仍然在线,我还有另一个与会话相关的问题?我很乐意帮助您在我的回答中添加有关
会话\u start()
的信息。它不一定要在这个特定的包含页面的顶部,但它应该在任何使用/设置会话值的页面的顶部。啊,我明白了!我还有一个问题,很简单,只是不适合我。。。想帮忙吗?去问吧
$info = mysql_fetch_array( $sql );
$answer = $info['id'];
$answer = $r['id'];