Can someone tell me what I am doing wrong here ...
This php runs a select query and then returns the result as a json encode.
It would seem the if($stmt->execute())
is not working correctly, if I put in an incorrect username and password I am getting [] instead of the "Incorrect email or password!", its not going into the else statement.
$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();
EDIT !
I tried checking for rows , but this gives me a new problem, the below results in Fatal error: Call to a member function 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)) {
// ...
}
}