如果查询为空,如何返回错误语句?

时间:2015-04-27 17:30:11

标签: prepared-statement

如果查询为空,我想回复一下。

session_start();

include 'inc/db.php'; 

$student_id = $_POST['username'];

$password = $_POST['password'];

if($stmt = $conn->prepare("SELECT p.parent_id,  ps.student_id, p.password 

 FROM parent p,

 parentstudent ps

 WHERE p.parent_id = ps.parent_id

 AND student_id = ?  limit 1")) {

   $stmt->bind_param("s", $student_id); 

   $stmt->execute(); 

   $stmt->bind_result($id, $student_id, $bindpassword);

   while ($stmt->fetch()) {

       if(password_verify($password, $bindpassword)){

    $_SESSION['parent_id']= $id;

    $_SESSION['student_id'] = $student_id;

    header("location: parent.php");

       }

    else 

    echo "The student ID  or password you have entered is inccorrect";

    }

   }

   $stmt->close();

1 个答案:

答案 0 :(得分:0)

因为你只有一行要从数据库中检索,你可以用if替换while(我删除了我之前的回复)

session_start();    
include 'inc/db.php';


$student_id = $_POST['username'];

$password = $_POST['password'];

$stmt = $conn->prepare("SELECT p.parent_id, ps.student_id, p.password
                        FROM parent p,
                        parentstudent ps
                        WHERE p.parent_id = ps.parent_id
                        AND student_id = ? limit 1") ;

$stmt->bind_param("i", $student_id);
$stmt->bind_result($id, $student_id, $bindpassword);

$stmt->execute();



if($stmt->fetch()) {

   if(password_verify($password, $bindpassword)){

$_SESSION['parent_id']= $id;

$_SESSION['student_id'] = $student_id;

header("location: parent.php");

   }
  else {

echo "The student ID  or password you have entered is inccorrect";

}
}
else {

echo "nothing was fetch";

}


$stmt->close();

我已经放了另外两个,一个是密码不正确的,一个是否在数据库中没有提取