PDO - 获取 COUNT(*) 的结果?
在新用户注册过程中,我试图查找用户名或用户电子邮件是否已在数据库中。为此,我想查找标识符(电子邮件或用户名)与数据库中的记录匹配的行数。如果我没有搞砸,唯一可能的返回值是 0 或 1。我的函数如下,但我需要帮助才能完成它。
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()
的某种变体,但这将是一个数组,对吗?
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;
}
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
(注意:我还没有测试过这个,我的工作机器上也没有安装 PHP 来测试它;我在 C#/Java 商店工作)
很可能,你会想要
$query->fetchColumn();
您还可以传递您想要的列号,但它默认为第 0 列。
(Note: I haven't tested this, nor do I have PHP installed on my work machine to test it; I work in a C#/Java shop)
Likely, you'd want
$query->fetchColumn();
You can also pass which column number you want, but it defaults to column 0.
您可以使用
->fetchColumn(0)
< /a> 从下一个(唯一的)行集中获取第 1 个且唯一的列。You could use
->fetchColumn(0)
to get the 1 and only column from the next (one and only) rowset.