방법: 데이터를 스트림으로 보내기
SQL Server Driver for PHP에서는 큰 개체를 서버에 보내기 위해 PHP 스트림을 사용합니다. 이 항목의 예제에서는 데이터를 스트림으로 보내는 방법을 보여 줍니다. 첫 번째 예제에서는 쿼리 실행 시 모든 스트림 데이터를 보내는 기본 동작을 보여 줍니다. 두 번째 예제에서는 한 번에 최대 8KB의 스트림 데이터를 서버로 보내는 방법을 보여 줍니다.
예
다음 예제에서는 AdventureWorks 데이터베이스의 Production.ProductReview 테이블에 행을 삽입합니다. PHP fopen 함수를 사용하여 고객 주석($comments)을 스트림으로 연 다음 쿼리 실행 시 서버로 스트리밍합니다.
이 예제에서는 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 = "INSERT INTO Production.ProductReview (ProductID,
ReviewerName,
ReviewDate,
EmailAddress,
Rating,
Comments)
VALUES (?, ?, ?, ?, ?, ?)";
/* Set the parameter values and put them in an array.
Note that $comments is opened as a stream. */
$productID = '709';
$name = 'Customer Name';
$date = date("Y-m-d");
$email = 'customer@name.com';
$rating = 3;
$comments = fopen( "data://text/plain,[ Insert lengthy comment here.]",
"r");
$params = array($productID, $name, $date, $email, $rating, $comments);
/* Execute the query. All stream data is sent upon execution.*/
$stmt = sqlsrv_query($conn, $tsql, $params);
if( $stmt === false )
{
echo "Error in statement execution.\n";
die( print_r( sqlsrv_errors(), true));
}
else
{
echo "The query was successfully executed.";
}
/* Free statement and connection resources. */
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
?>
다음 예제는 이전 예제와 같지만 실행 시 모든 스트림을 보내는 기본 동작이 해제되어 있습니다. 이 예제에서는 sqlsrv_send_stream_data를 사용하여 스트림 데이터를 서버에 보냅니다. sqlsrv_send_stream_data를 호출할 때마다 최대 8KB의 데이터가 전송됩니다. 스크립트에서는 sqlsrv_send_stream_data의 호출 횟수를 계산하여 콘솔에 표시합니다.
이 예제에서는 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 = "INSERT INTO Production.ProductReview (ProductID,
ReviewerName,
ReviewDate,
EmailAddress,
Rating,
Comments)
VALUES (?, ?, ?, ?, ?, ?)";
/* Set the parameter values and put them in an array.
Note that $comments is opened as a stream. */
$productID = '709';
$name = 'Customer Name';
$date = date("Y-m-d");
$email = 'customer@name.com';
$rating = 3;
$comments = fopen( "data://text/plain,[ Insert lengthy comment here.]",
"r");
$params = array($productID, $name, $date, $email, $rating, $comments);
/* Turn off the default behavior of sending all stream data at
execution. */
$options = array("SendStreamParamsAtExec" => 0);
/* Execute the query. */
$stmt = sqlsrv_query($conn, $tsql, $params, $options);
if( $stmt === false )
{
echo "Error in statement execution.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Send up to 8K of parameter data to the server with each call to
sqlsrv_send_stream_data. Count the calls. */
$i = 1;
while( sqlsrv_send_stream_data( $stmt))
{
echo "$i call(s) made.\n";
$i++;
}
/* Free statement and connection resources. */
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
?>
이 항목의 예제에서는 문자 데이터를 서버에 보내지만 모든 형식의 데이터를 스트림으로 보낼 수 있습니다. 예를 들어, 이 항목에서 보여 주는 방법을 사용하여 이진 형식의 이미지를 스트림으로 보낼 수도 있습니다.