如何:使用 SQLSRV 驱动程序以流的形式检索字符数据

下载 PHP 驱动程序

以流的形式检索数据仅在 Microsoft Drivers for PHP for SQL Server 的 SQLSRV 驱动程序中可用,在 PDO_SQLSRV 驱动程序中不可用。

SQLSRV 驱动程序利用 PHP 流从服务器检索大量数据。 本主题中的示例演示如何以流的形式检索字符数据。

示例

以下示例从 AdventureWorks 数据库的 Production.ProductReview 表中检索行。 返回行的 Comments 字段以流的形式检索,并通过使用 PHP fpassthru 函数进行显示

通过使用 sqlsrv_fetchsqlsrv_get_field (其中返回类型指定为字符流)来完成以流的形式检索数据操作。 返回类型通过使用常量 SQLSRV_PHPTYPE_STREAM 指定。 有关 sqlsrv 常量的信息,请参阅常量 (Microsoft Drivers for PHP for SQL Server)

该示例假定已在本地计算机上安装了 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 数据类型的信息,请参阅 Default PHP Data Types。 有关如何指定 PHP 返回类型的信息,请参阅 How to: Specify PHP Data Types

另请参阅

检索数据

使用 SQLSRV 驱动程序以流的形式检索数据

文档中相关的代码示例