我正在尝试制作一个登录脚本,其中我使用 PDO MYSQL 进行数据库连接。这是我的代码:
$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;
当email_address
和password
都正确时,$status
正在打印1
。但是,如果 email_address
或 password
与数据库值不匹配,则不会打印 $status
的任何内容。我认为至少应该打印 0
。但我没有明白我错在哪里?
最佳答案
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
因为您正在 $status
变量中存储 bool 值。 bool 值在 PHP 中有其自己的行为。
boolean value true will always print `1`
boolean value false will print nothing.
试试这个
echo true;
//prints 1
echo false;
//prints nothing
如果你想打印0,那么你可以这样做。
$status = ($result == true) ? 1 : 0;
如果您不知道的话,上面的代码是三元运算符。这与
相同if($result)
$status = 1;
else
$status = 0;
使用条件语句时,1 将始终被视为 bool true,0 将始终被视为 false。
我希望这能回答您的问题。
关于php - 在PDO mysql连接中,pdo查询输出对于mysql null结果集来说什么都没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10793789/