문제 설명
PDO ‑ COUNT() 결과를 얻습니까? (PDO ‑ get the result of a COUNT()?)
새 사용자 등록 과정에서 사용자 이름이나 사용자 이메일이 이미 db에 있는지 확인하려고 합니다. 그렇게 하려면 식별자(이메일 또는 사용자 이름)가 데이터베이스의 레코드와 일치하는 행 수를 찾고 싶습니다. 내가 망치지 않으면 가능한 반환 값은 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()
의 변형이라고 생각하지만 배열이 되겠죠?
참조 솔루션
방법 1:
(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.
방법 2:
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;
(by JDelage、Powerlord、sholsinger)