sqlsrv_fetch

결과 집합의 다음 행을 읽을 수 있도록 합니다. sqlsrv_get_field를 사용하여 행의 필드를 읽을 수 있습니다.

구문

sqlsrv_fetch( resource $stmt[, row[, ]offset])

매개 변수

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

참고

문을 실행해야만 결과를 검색할 수 있습니다. 문을 실행하는 방법은 sqlsrv_querysqlsrv_execute를 참조하십시오.

row [옵션]: 1.1 버전에 추가되었습니다. 다음 값 중 하나로, 스크롤할 수 있는 커서를 사용하는 결과 집합에서 액세스할 행을 지정합니다.

  • SQLSRV_SCROLL_NEXT
  • SQLSRV_SCROLL_PRIOR
  • SQLSRV_SCROLL_FIRST
  • SQLSRV_SCROLL_LAST
  • SQLSRV_SCROLL_ABSOLUTE
  • SQLSRV_SCROLL_RELATIVE

이러한 값에 대한 자세한 내용은 커서 유형 지정 및 행 선택을 참조하십시오. SQL Server Driver for PHP 1.1 버전에는 스크롤할 수 있는 커서 지원이 추가되었습니다.

offset [옵션]: SQLSRV_SCROLL_ABSOLUTE 및 SQLSRV_SCROLL_RELATIVE와 함께 사용되어 검색할 행을 지정합니다. 결과 집합의 첫 번째 레코드는 0입니다.

반환 값

결과 집합의 다음 행이 성공적으로 검색된 경우 true가 반환됩니다. 결과 집합에 더 이상 결과가 없을 경우 null이 반환됩니다. 오류가 발생할 경우 false가 반환됩니다.

다음 예제에서는 sqlsrv_fetch를 사용하여 검토자의 이름과 제품 검토 내용이 들어 있는 데이터 행을 검색합니다. 결과 집합에서 데이터를 검색하려면 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 SQL Server type nvarchar. */
$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)