如果查询为空,我如何 return 一个错误语句?

How do i return an error statement if the query is empty?

如果查询为空,我想回显一些内容。

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();

因为你只有一行要从数据库中检索,所以你可以用 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();

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