I have the following function:
function authenticate($req, $resp, $args) {
$credentials = json_decode($req->getBody());
$sql = "SELECT usr_password FROM ict_users WHERE usr_username='".$credentials->username."'";
try {
$db = DB_Connection();
$stmt = $db->query($sql);
$password = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
if (password_verify($credentials->password, $password[0]->usr_password)) {
echo 'Valid password !';
} else {
echo 'Invalide password !.';
}
}
If I put return;
after $db = null;
will it stop executing my function? Or is there a better way to stop the function when an error is caught?
You have an try {}catch(){}
block, why not throw new Exeption();
instead of return null
. And return null
is not done if you want "stop the function when an error is caught", you just returning null
from the function.