SQL Server Driver for PHP에서는 서버에서 대량의 데이터를 검색하기 위해 PHP 스트림을 사용합니다. 이 항목의 예제에서는 문자 데이터를 스트림으로 검색하는 방법을 보여 줍니다.
예
다음 예제에서는 AdventureWorks 데이터베이스의 Production.ProductReview 테이블에서 행을 검색합니다. 반환된 행의 Comments 필드는 스트림으로 검색되며 PHP fpassthru 함수를 사용하여 표시됩니다.
반환 형식을 문자 스트림으로 지정하여 sqlsrv_fetch 및 sqlsrv_get_field를 사용하면 데이터가 스트림으로 검색됩니다. 반환 형식은 SQLSRV_PHPTYPE_STREAM 상수를 사용하여 지정합니다. sqlsrv 상수에 대한 자세한 내용은 SQLSRV 상수를 참조하십시오.
이 예제에서는 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 the Transact-SQL query. */
$tsql = "SELECT ReviewerName,
CONVERT(varchar(32), ReviewDate, 107) AS [ReviewDate],
Rating,
Comments
FROM Production.ProductReview
WHERE ProductReviewID = ? ";
/* Set the parameter value. */
$productReviewID = 1;
$params = array( $productReviewID);
/* Execute the query. */
$stmt = sqlsrv_query($conn, $tsql, $params);
if( $stmt === false )
{
echo "Error in statement execution.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Retrieve and display the data. The first three fields are retrieved
as strings and the fourth as a stream with character encoding. */
if(sqlsrv_fetch( $stmt ) === false )
{
echo "Error in retrieving row.\n";
die( print_r( sqlsrv_errors(), true));
}
echo "Name: ".sqlsrv_get_field( $stmt, 0 )."\n";
echo "Date: ".sqlsrv_get_field( $stmt, 1 )."\n";
echo "Rating: ".sqlsrv_get_field( $stmt, 2 )."\n";
echo "Comments: ";
$comments = sqlsrv_get_field( $stmt, 3,
SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_CHAR));
fpassthru($comments);
/* Free the statement and connection resources. */
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
?>
처음 세 필드에 대해서는 PHP 반환 형식이 지정되지 않았으므로 각 필드는 기본 PHP 형식에 따라 반환됩니다. 기본 PHP 데이터 형식에 대한 자세한 내용은 기본 PHP 데이터 형식을 참조하십시오. PHP 반환 형식을 지정하는 방법은 방법: PHP 데이터 형식 지정을 참조하십시오.