PDOStatement::columnCount
(PHP 5 >= 5.1.0, PHP 7, PHP 8, PECL pdo >= 0.2.0)
PDOStatement::columnCount — 결과 집합의 열 수를 반환합니다.
설명
public PDOStatement::columnCount(): int
PDOStatement::columnCount()를 사용하여 PDOStatement 개체가 나타내는 결과 집합의 열 수를 반환합니다.
PDOStatement 객체가 PDO::query()에서 반환된 경우 열 개수를 즉시 사용할 수 있습니다.
PDOStatement 객체가 PDO::prepare()에서 반환된 경우 PDOStatement::execute()를 호출할 때까지 정확한 열 수를 사용할 수 없습니다.
매개변수
이 함수에는 매개변수가 없습니다.
반환 값
결과 집합이 비어 있는 경우에도 PDOStatement 개체가 나타내는 결과 집합의 열 수를 반환합니다. 결과 집합이 없으면 PDOStatement::columnCount()는 0
을 반환합니다.
Examples
예제 #1 열 계산
이 예제는 PDOStatement::columnCount()가 결과 세트가 있거나 없는 상태에서 어떻게 작동하는지 보여줍니다.
<?php
$dbh = new PDO('odbc:sample', 'db2inst1', 'ibmdb2');
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
/* Count the number of columns in the (non-existent) result set */
$colcount = $sth->columnCount();
print("Before execute(), result set has $colcount columns (should be 0)\n");
$sth->execute();
/* Count the number of columns in the result set */
$colcount = $sth->columnCount();
print("After execute(), result set has $colcount columns (should be 2)\n");
?>
위의 예는 다음을 출력합니다.
Before execute(), result set has 0 columns (should be 0) After execute(), result set has 2 columns (should be 2)
기타
- PDO::prepare() - 실행할 명령문을 준비하고 명령문 개체를 반환합니다.
- PDOStatement::execute() - 준비된 명령문을 실행합니다.
- PDOStatement::rowCount() - 마지막 SQL 문의 영향을 받은 행 수를 반환합니다.