PDO-获得COUNT(*)的结果? [英] PDO - get the result of a COUNT(*)?
问题描述
在新的用户注册过程中,我正在尝试查找数据库中是否已存在用户名或用户电子邮件.为此,我想查找标识符(电子邮件或用户名)与数据库中的记录匹配的行数.如果我不搞清楚,唯一的可能返回值是0或1.我的函数在下面,但我需要帮助来完成它.
During the new user registration process, I'm trying to find whether a user name or a user email are already in the db. To do that, I want to find the number of rows where the identifier (email or user name) matches records in the database. If I don't screw up, the only possible return values are 0 or 1. My function is below, but I need help to complete it.
function checkUserExists($userIdentifier, $tableColName){
$dbConnection=$this->dbInstance->createConnexion();
$query=$dbConnection->prepare("SELECT count(*) FROM users WHERE ".$tableColName."= :userIdentifier");
$query->bindParam(":userIdentifier", $userIdentifier);
$result=$query->execute();
if( ????? >0){
$return false;
} else return true;
}
我很傻,我不确定如何获得该计数.我想这是$query->fetch()
的某种变体,但这将是一个数组吗?
Silly of me, I'm not sure how to get that count number. I suppose it's some variation of $query->fetch()
, but that's going to be an array right?
推荐答案
您可以使用 ->fetchColumn(0)
从下一个(唯一的)行集中获取唯一的1列.
You could use ->fetchColumn(0)
to get the 1 and only column from the next (one and only) rowset.
if ( $query->fetchColumn(0) > 0 ){
return false;
} else return true;
这篇关于PDO-获得COUNT(*)的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!