PHP mysqli或stmt如何在php变量中存储查询值现在正常工作

时间:2015-03-29 17:16:11

标签: php mysqli

我正在尝试从查询中获取变量并使用此查询将它们存储为cookie:

$query="SELECT id,username,password FROM employee where email='$email' AND password='$password' Limit 1";

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

if($result->num_rows == 1){
    $stmt = $mysqli->prepare($query);
    $stmt->execute();
    $stmt->store_result();
    $stmt->bind_result($user_id, $username, $db_password);
    $user_browser = $_SERVER['HTTP_USER_AGENT'];
    $_SESSION['user_id'] = $user_id;
    //used echo to check stored variables

    echo "user_id=".$_SESSION['user_id'];
    //used echo here to check the query result

    $_SESSION['username'] = $username;
    echo "username=".$username;
    $_SESSION['login_string'] = hash('sha512', 
    $password . $user_browser);
        // Login successful.
        return true;
    }
     else{
        // Password is not correct
        // We record this attempt in the database
        $now = time();
        $mysqli->query("INSERT INTO login_attempts(user_id, time)
                        VALUES ('$user_id', '$now')");
        return false;
    }
}

我在网站上看到的是: user_id=0username=

1 个答案:

答案 0 :(得分:0)

这就是我如何解决我的问题:

$quer="SELECT id,username FROM employee where email=?";

if($result->num_rows == 1){

$stmt = $mysqli->prepare($quer);
$stmt->bind_param('i',$email);
$stmt->execute();
$stmt->bind_result($user_id,$username);

添加了:

while($stmt->fetch()) {}

有关:

echo "Cookies username:".$_SESSION['username']." ";
echo "db username".$username;

得到了结果:

Cookies username:test_user db usernametest_user

感谢帮助人员。