다음을 통해 공유


sqlsrv_num_fields

활성 결과 집합의 필드 수를 검색합니다. sqlsrv_num_fields는 실행 전이나 실행 후에 준비된 문에서 호출할 수 있습니다.

구문

sqlsrv_num_fields( resource $stmt)

매개 변수

$stmt: 대상 결과 집합이 활성 상태로 있는 문입니다.

반환 값

활성 결과 집합의 필드 수를 나타내는 정수 값입니다. 오류가 발생하는 경우 부울 값 false가 반환됩니다.

다음 예제에서는 Adventureworks 데이터베이스의 HumanResources.Department 테이블에서 최상위 세 개의 행에 있는 모든 필드를 검색하는 쿼리를 실행합니다. sqlsrv_num_fields 함수는 결과 집합의 필드 수를 결정하며 반환된 각 행의 필드를 반복 처리하여 데이터를 표시할 수 있도록 합니다.

이 예제에서는 SQL Server와 AdventureWorks 데이터베이스가 로컬 컴퓨터에 설치되어 있다고 가정합니다. 명령줄에서 이 예제를 실행하면 모든 출력이 콘솔에 기록됩니다.

<?php
/* Connect to the local server using Windows Authentication and 
specify the AdventureWorks database as the database in use. */
$serverName = "(local)";
$connectionInfo = array( "Database"=>"AdventureWorks");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false )
{
     echo "Could not connect.\n";
     die( print_r( sqlsrv_errors(), true));
}

/* Define and execute the query. */
$tsql = "SELECT TOP (3) * FROM HumanResources.Department";
$stmt = sqlsrv_query($conn, $tsql);
if( $stmt === false)
{
     echo "Error in executing query.\n";
     die( print_r( sqlsrv_errors(), true));
}

/* Retrieve the number of fields. */
$numFields = sqlsrv_num_fields( $stmt );

/* Iterate through each row of the result set. */
while( sqlsrv_fetch( $stmt ))
{
     /* Iterate through the fields of each row. */
     for($i = 0; $i < $numFields; $i++)
     {
          echo sqlsrv_get_field($stmt, $i, 
                   SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR))." ";
     }
     echo "\n";
}

/* Free statement and connection resources. */
sqlsrv_free_stmt( $stmt );
sqlsrv_close( $conn );
?>

참고 항목

참조

sqlsrv_field_metadata

개념

설명서에 포함된 코드 예제 정보

관련 자료

API 참조(SQL Server Driver for PHP)