-2

我写的php

$query = "SELECT privilege , no, userName FROM  $tableName WHERE userName =   

'$inputAccount' and password = '$inputPassword'";

$result = $mysqli->query($query);

if($result){
       while ($row = $result->fetch_assoc()) {
        printf ("%s %s %s\n", $row["privilege"], $row["no"], $row["userName"]);
    }
}

结果是user 2 test

session_start();
$_SESSION["privilege"]=$row["privilege"];
$_SESSION["no"]=$row["no"];
$_SESSION["userName"]=$row["userName"]

echo $_SESSION["privilege"] ;
echo $_SESSION["no"];
echo $_SESSION["userName"];

但是结果一无所获,我已经修复了很长时间,但找不到原因?

提前谢谢。

4

2 回答 2

2

使用下面的代码,问题是您没有将 sql 输出存储在变量中并在 while 循环之外访问结果。

session_start();
$query = "SELECT privilege , no, userName FROM  $tableName WHERE userName =   

'$inputAccount' and password = '$inputPassword'";

$result = $mysqli->query($query);

if($result){
       while ($row = $result->fetch_assoc()) {
        //printf ("%s %s %s\n", $row["privilege"], $row["no"], $row["userName"]);
       $pr = $row["privilege"];
       $no = $row["no"];
       $name = $row["userName"];
    }
}

$_SESSION["privilege"]=$pr;
$_SESSION["no"]=$no;
$_SESSION["userName"]=$name

echo $_SESSION["privilege"] ;
echo $_SESSION["no"];
echo $_SESSION["userName"];
于 2013-02-04T05:10:57.340 回答
1

假设您在循环之后分配值,将$_SESSION 处于该阶段。while$rowfalse

于 2013-02-04T05:10:54.747 回答