有人可以告诉我这里我做错了什么......
这个php运行一个select查询,然后将结果作为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();
编辑!
我尝试检查行,但这给了我一个新问题,下面的结果就是这样 致命错误:调用成员函数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)) {
// ...
}
}
答案 0 :(得分:2)
它就像那样返回只是因为语句仍然成功,即使它没有返回任何行。例如,当SQL中出现错误时,查询失败。
相反,您需要计算返回的行数。
答案 1 :(得分:0)
试试这个:
$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);
$stmt->execute();
$stmt->store_results();
$resultRows = $stmt->num_rows;
$stmt->free_result();
if($resultRows >= 1){
$result = $stmt->get_result();
$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();
答案 2 :(得分:-1)
如果第一个返回的行...
,我设法通过运行单独的查询来使其工作$email = $_POST['email'];
$password = $_POST['password'];
//check user exists
$qry = "SELECT * FROM users WHERE email = ? AND password = ? ";
$stmt = $mysqli->prepare($qry);
$stmt->bind_param('ss', $email, $password);
$stmt->execute();
$stmt->store_result();
$resultRows = $stmt->num_rows;
$stmt->free_result();
if($resultRows >= 1){
$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);
$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();