我目前正在为我的系统构建数据库,并希望通过对来自 php 的每个请求使用存储函数和存储过程来创建数据库的抽象。 我的问题是我有一个功能可以检查电子邮件是否存在,如果电子邮件存在则返回 true,否则返回 false。现在我如何在 php 中调用 pdo 后检查 bool 返回值。我的 PHP 代码是
$connection = new DB_CONNECTION();
$sql = "SELECT email_exists(:email) ";
$placeholder = array(":email" => $userMail );
$statement = $connection->prepare_query( $sql );
$result = $statement->execute($placeholder);
$result = $statement->fetch();
echo $result;
最佳答案
只需给返回值一个别名
,它就可以通过数组中的索引访问。
$connection = new DB_CONNECTION();
$sql = "SELECT email_exists(:email) as da_count ";
$placeholder = array(":email" => $userMail );
$statement = $connection->prepare_query( $sql );
$result = $statement->execute($placeholder);
$result = $statement->fetch();
echo $result['da_count'];
您可以根据需要对其进行条件化;
if(empty($result['da_count'])) {
echo 'Email doesnt exist';
} else {
echo 'Email does exist';
}
关于php - 检查pdo中mysql存储函数的 bool 返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45334408/