sqlsrv_get_field

현재 행의 지정된 필드에서 데이터를 검색합니다. 필드 데이터에는 순서대로 액세스해야 합니다. 예를 들어 두 번째 필드의 데이터에 액세스한 후 첫 번째 필드의 데이터에 액세스할 수는 없습니다.

구문

sqlsrv_get_field( resource $stmt, int $fieldIndex [, int $getAsType])

매개 변수

$stmt: 실행된 문에 해당하는 문 리소스입니다.

$fieldIndex: 검색할 필드의 인덱스입니다. 인덱스는 0에서 시작합니다.

$getAsType[옵션]: 반환되는 데이터의 PHP 데이터 형식을 확인하는 SQLSRV 상수(SQLSRV_PHPTYPE_*)입니다. 지원되는 데이터 형식에 대한 자세한 내용은 SQLSRV 상수를 참조하십시오. 반환 형식을 지정하지 않으면 기본 PHP 형식이 반환됩니다. 기본 PHP 형식에 대한 자세한 내용은 기본 PHP 데이터 형식을 참조하십시오. PHP 데이터 형식을 지정하는 방법은 방법: PHP 데이터 형식 지정을 참조하십시오.

반환 값

필드 데이터입니다. $getAsType 매개 변수를 사용하여 반환되는 데이터의 PHP 데이터 형식을 지정할 수 있습니다. 반환 데이터 형식을 지정하지 않으면 기본 PHP 데이터 형식이 반환됩니다. 기본 PHP 형식에 대한 자세한 내용은 기본 PHP 데이터 형식을 참조하십시오. PHP 데이터 형식을 지정하는 방법은 방법: PHP 데이터 형식 지정을 참조하십시오.

다음 예제에서는 의견을 제출한 고객의 이름과 제품 검토 내용이 들어 있는 데이터 행을 검색합니다. 결과 집합에서 데이터를 검색하기 위해 sqlsrv_get_field가 사용되었으며, 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));
}

/* Set up and execute the query. Note that both ReviewerName and
Comments are of the SQL Server nvarchar type. */
$tsql = "SELECT ReviewerName, Comments 
         FROM Production.ProductReview
         WHERE ProductReviewID=1";
$stmt = sqlsrv_query( $conn, $tsql);
if( $stmt === false )
{
     echo "Error in statement preparation/execution.\n";
     die( print_r( sqlsrv_errors(), true));
}

/* Make the first row of the result set available for reading. */
if( sqlsrv_fetch( $stmt ) === false )
{
     echo "Error in retrieving row.\n";
     die( print_r( sqlsrv_errors(), true));
}

/* Note: Fields must be accessed in order.
Get the first field of the row. Note that no return type is
specified. Data will be returned as a string, the default for
a field of type nvarchar.*/
$name = sqlsrv_get_field( $stmt, 0);
echo "$name: ";

/*Get the second field of the row as a stream.
Because the default return type for a nvarchar field is a
string, the return type must be specified as a stream. */
$stream = sqlsrv_get_field( $stmt, 1, 
                            SQLSRV_PHPTYPE_STREAM( SQLSRV_ENC_CHAR));
while( !feof( $stream))
{ 
    $str = fread( $stream, 10000);
    echo $str;
}

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

참고 항목

개념

데이터 검색 함수 비교
설명서에 포함된 코드 예제 정보

관련 자료

API 참조(SQL Server Driver for PHP)
데이터 검색