这是我的php代码,它必须工作,但执行错误! 有什么问题?! 我是一名新的PDO程序员
<?php
if(isset($_POST['login'])){
if(empty($_POST['email']) || empty($_POST['password'])){
$_SESSION['msg_login']='<script type="text/javascript">alert("error"); </script>';
header("location:index.php");
} else {
$sql = "SELECT * FROM `user_login` WHERE `user` = :user AND `pass` = :pass ;";
$result = $connect->prepare($sql);
$pass = hash_pass($_POST['password']);
$query = $result->execute(array(
"user"=>$_POST['email'],"pass"=>$pass));
$num1 = $result->fetchColumn();
if ($num1==1){
$fetch = $connect->query($sql);
$fetch->execute(array("user"=>$_POST['email'],"pass"=>$pass));
while ($rows=$fetch->fetch(PDO::FETCH_ASSOC)) {
echo 'ok';
}
}
}
}
错误:致命错误:在非对象上调用成员函数execute()
答案 0 :(得分:1)
<?php
$sql = "SELECT
*
FROM
`user_login`
WHERE
`user` = :user AND
`pass` = :pass";
$inputs = array(
":user" => $_POST['email'],
":pass" => hash_pass($_POST['password'])
);
$stmt = $connect->prepare($sql);
$stmt->execute($inputs);
$stmt->setFetchMode(PDO::FETCH_ASSOC);
if ($user = $stmt->fetch()) {
var_dump($user);
} else {
echo "bad login";
}