I am trying to make a login script where for database connection I am using PDO MYSQL. Here is my code:
$query = "SELECT uid,uname from user where email_address='abc@gmail.com' AND password='".md5('abcacb')."'";
try {
$dbh = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME,DB_USER,DB_PASS);
$stmt = $dbh->query($query);
$result = $stmt->fetch(PDO::FETCH_ASSOC);
if(!$result)
$status = FALSE;
else
$status = TRUE;
$dbh = null;
}
catch(PDOException $e){
echo $e->getMessage();
}
print $status;
When email_address & password both are correct $status is printing 1. But if email_address or password mismatch from the database value it’s not printing anything for $status. I assume that at least it should print 0. But I am not getting where I am wrong?
since you are sotring the boolean values in
$statusvariable. boolean values have it’s own behavior in PHP.try this
if you want 0 to be printed, then you can do it like this.
above code is the ternary operator if you not aware. which is same as
while using conditional statement, 1 will always be treated as boolean true and 0 as false.
i hope this answer your question.