共用方式為


如何:以資料流傳送資料

SQL Server Driver for PHP 會利用 PHP 資料流來傳送大型物件給伺服器。本主題的範例示範如何以資料流傳送資料。第一個範例示範預設行為,也就是在執行查詢時傳送所有資料流資料。第二個範例示範如何一次將最多 8 KB 的資料流資料傳送給伺服器。

範例

下列範例會將資料列插入 AdventureWorks 資料庫的 Production.ProductReview 資料表。客戶意見 ($comments) 會使用 PHP fopen 函數來以資料流形式開啟,然後在執行查詢時以資料流方式傳送給伺服器。

此範例假設 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 呼叫最多會傳送 8 KB 的資料。此指令碼會計算 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);
?>

雖然本主題的範例會傳送字元資料給伺服器,但是任何格式的資料都可以當做資料流傳送。例如,您也可以使用本主題所示範的技術,使用二進位格式來以資料流傳送影像。

另請參閱

概念

有關文件集中的程式碼範例

其他資源

更新資料 (SQL Server Driver for PHP)
將資料當做資料流擷取