有人可以告诉我我在这里做错了什么......
此 php 运行选择查询,然后将结果作为 json 编码返回。if($stmt->execute())
如果我输入错误的用户名和密码,我会得到 [] 而不是“不正确的电子邮件或密码!”,这似乎无法正常工作,它不会进入 else 语句。
$email = $_POST['email'];
$password = $_POST['password'];
//check user exists
$qry = "SELECT u.uid, u.name, u.email, u.registration_date, u.updated_at, d.device_name, d.device_registration_date
FROM users u
LEFT JOIN devices d
ON d.parent_id = u.uid
WHERE u.email = ? AND u.password = ? ";
$stmt = $mysqli->prepare($qry);
$stmt->bind_param('ss', $email, $password);
if ($stmt->execute()) {
$result = $stmt->get_result();
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$response["success"] = 1;
$response["device"][]=$row;
}
echo json_encode($response);
} else {
// user not found
$response["fail"] = 0;
$response["error_msg"] = "Incorrect email or password!";
echo json_encode($response);
exit;
}
$stmt->close();
编辑 !
我尝试检查 rows ,但这给了我一个新问题,以下导致致命错误:调用成员函数 fetch_array()
$stmt = $mysqli->prepare($qry);
$stmt->bind_param('ss', $email, $password);
$stmt->execute();
$stmt->fetch();
$stmt->store_result();
if ($stmt->num_rows >= 0) {
$result = $stmt->get_result();
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
// ...
}
}