Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDO - get the result of a COUNT(*)?

Tags:

php

pdo

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?

like image 801
JDelage Avatar asked Dec 28 '22 04:12

JDelage


1 Answers

(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.

like image 178
Powerlord Avatar answered Dec 30 '22 17:12

Powerlord