Reputation: 964
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='[email protected]' 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?
Upvotes: 0
Views: 161
Reputation: 25735
When email_address & password both are correct $status is printing 1. But if email_address or password is mismatch from database value it's not printing any thing for $status
since you are sotring the boolean values in $status
variable. boolean values have it's own behavior in PHP.
boolean value true will always print `1`
boolean value false will print nothing.
try this
echo true;
//prints 1
echo false;
//prints nothing
if you want 0 to be printed, then you can do it like this.
$status = ($result == true) ? 1 : 0;
above code is the ternary operator if you not aware. which is same as
if($result)
$status = 1;
else
$status = 0;
while using conditional statement, 1 will always be treated as boolean true and 0 as false.
i hope this answer your question.
Upvotes: 1
Reputation: 1413
It sounds like you're expecting the wrong thing from print-- try using print_r or var_dump for debug printing instead.
Upvotes: 1