I'm having problems with PDO.
Currently, I'm working on a simple login page, without considering about SQL injection yet, but I cannot find a way to get rid of Notice: Trying to get property of non-object in C:\xampp\htdocs\Penston\process.php on line 60
I have a database which there is an email abc@m.com
and password 5555
. When I do
SELECT * FROM `users` WHERE `email` = 'abc@m.com' AND `password` = '5555'
it works perfectly fine. But when I change to
SELECT * FROM `users` WHERE `email` = 'abc@m.com' AND `password` = 'wrong'
in order to check how it works when the incorrect password is entered, it gives me
Notice: Trying to get property of non-object in C:\xampp\htdocs\Penston\process.php on line 60
where line 60 is print $result->Password;
. I understand that because it has no row in the database that contains the entered user and password, so that it has no object to return. But, how to catch the exception of that? I don't want users to see this unhandled message. I don't understand why catch
statement doesn't work.
Thus, if I want to handle this and show the message that incorrect username/password when the entered username and password is not found, what should I do?
Here is the code,
try {
$conn = new PDO("mysql:host=$SQL_servername;dbname=penston;", $SQL_username, $SQL_password);
$conn -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT * FROM `users` WHERE `email` = 'abc@m.com' AND `password` = '5555'");
$temp = $stmt->execute();
$result = $stmt->fetch(PDO::FETCH_OBJ);
print $result->Password;
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
This might be an easy question but I am really new to PDO, so please help me.
Thanks.
Edit 1: I forgot to tell that the clear password is just for testing, I have the encrypted password but I just want to test so I use explicit password like this.