PDO - COUNT(*) 결과를 얻습니까? (PDO - get the result of a COUNT(*)?)


문제 설명

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 JDelagePowerlordsholsinger)

참조 문서

  1. PDO ‑ get the result of a COUNT(*)? (CC BY‑SA 2.5/3.0/4.0)

#PHP #pdo






관련 질문

bash의 rsync가 php 생성 --exclude-from 파일을 구문 분석하지 않음 (rsync in bash not parsing php-generated --exclude-from file)

PHP 배열 값 저장 (PHP Save array value)

검색으로 배열에서 특정 데이터 가져오기 (get specific datas from a array by a search)

창 서비스를 사용하여 PHP 파일 트리거 (Trigger a php file using window service)

yii2 컨트롤러 작업 주입은 어떻게 작동합니까? (How does the yii2 Controller action injection works)

php와 drupal을 사용하여 pdf를 텍스트로 변환 (pdf to text convert using php and drupal)

PHP에서 카테고리 및 하위 카테고리 목록 검색 (Retrieve Category & Subcategory list in PHP)

PDO - COUNT(*) 결과를 얻습니까? (PDO - get the result of a COUNT(*)?)

PHP - MySQL 쿼리 문제 (PHP - Mysql query problem)

제품용 Reviews.io API의 Foreach 루프 (Foreach loop in Reviews.io API for Products)

숫자를 나누고 점 뒤에 하나의 숫자를 유지하십시오. (Split the number and keep one number after the dot)

내 메시지 입력이 데이터베이스에 들어가지 않습니다. (My message input doesn't get into database)







코멘트