row
[選用]:已在 1.1 版中新增。 下列其中一個值,指定要在使用可捲動資料指標的結果集內存取的資料列。 (如果指定
row
,則必須明確指定
fetchtype
,即使您指定預設值亦然。)
SQLSRV_SCROLL_NEXT
SQLSRV_SCROLL_PRIOR
SQLSRV_SCROLL_FIRST
SQLSRV_SCROLL_LAST
SQLSRV_SCROLL_ABSOLUTE
SQLSRV_SCROLL_RELATIVE
如需這些值的詳細資訊,請參閱
指定資料指標類型及選取資料列
。 Microsoft Drivers for PHP for SQL Server 1.1 版新增了可捲動的資料指標支援。
offset
[選用]:與 SQLSRV_SCROLL_ABSOLUTE 和 SQLSRV_SCROLL_RELATIVE 搭配使用,以指定要擷取的資料列。 結果集內的第一個記錄為 0。
如果擷取資料列,則會傳回
陣列
。 如果沒有更多資料列可擷取,則會傳回
null
。 若發生錯誤,將會傳回
false
。
根據
$fetchType
參數的值,傳回的
陣列
可以是數值索引
陣列
和 (或) 關聯
陣列
。 根據預設,會傳回同時具有數值和關聯索引鍵的
陣列
。 傳回之陣列中的資料類型值,將是預設的 PHP 資料類型。 如需有關預設 PHP 資料類型的詳細資訊,請參閱
Default PHP Data Types
。
如果傳回沒有名稱的資料行,則陣列元素的關聯索引鍵會是空字串 ("")。 例如,請考量下列將值插入資料庫資料表中,並擷取伺服器產生之主索引鍵的 Transact-SQL 陳述式:
INSERT INTO Production.ProductPhoto (LargePhoto) VALUES (?);
SELECT SCOPE_IDENTITY()
如果以關聯陣列的形式擷取此陳述式之 SELECT SCOPE_IDENTITY() 部分所傳回的結果集,傳回值的索引鍵將會是空字串 (""),因為傳回的資料行沒有名稱。 若要避免此狀況,您可以用數值陣列的形式擷取結果,或是在 Transact-SQL 陳述式中為傳回的資料行指定名稱。 下列陳述式是在 Transact-SQL 中指定資料行名稱的其中一種方式:
SELECT SCOPE_IDENTITY() AS PictureID
如果結果集包含多個沒有名稱的資料行,則最後一個未命名資料行的值將會指派給空字串 ("") 索引鍵。
關聯陣列範例
下列範例會以關聯 陣列的形式擷取結果集的每個資料列。 此範例假設本機電腦上已安裝 SQL Server 和 AdventureWorks 資料庫。 從命令列執行範例時,所有輸出都會寫入至主控台。
/* 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. */
$tsql = "SELECT FirstName, LastName
FROM Person.Contact
WHERE LastName='Alan'";
$stmt = sqlsrv_query( $conn, $tsql);
if( $stmt === false)
echo "Error in query preparation/execution.\n";
die( print_r( sqlsrv_errors(), true));
/* Retrieve each row as an associative array and display the results.*/
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC))
echo $row['LastName'].", ".$row['FirstName']."\n";
/* Free statement and connection resources. */
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
索引陣列範例
下列範例會以數值索引陣列的形式擷取結果集的每個資料列。
範例會針對具有指定的日期,以及存貨數量 (StockQty) 小於指定值的產品,從 AdventureWorks 資料庫的 Purchasing.PurchaseOrderDetail 資料表中擷取產品資訊。
此範例假設本機電腦上已安裝 SQL Server 和 AdventureWorks 資料庫。 從命令列執行範例時,所有輸出都會寫入至主控台。
/* 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));
/* Define the query. */
$tsql = "SELECT ProductID,
UnitPrice,
StockedQty
FROM Purchasing.PurchaseOrderDetail
WHERE StockedQty < 3
AND DueDate='2002-01-29'";
/* Execute the query. */
$stmt = sqlsrv_query( $conn, $tsql);
if ( $stmt )
echo "Statement executed.\n";
echo "Error in statement execution.\n";
die( print_r( sqlsrv_errors(), true));
/* Iterate through the result set printing a row of data upon each
iteration.*/
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC))
echo "ProdID: ".$row[0]."\n";
echo "UnitPrice: ".$row[1]."\n";
echo "StockedQty: ".$row[2]."\n";
echo "-----------------\n";
/* Free statement and connection resources. */
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
sqlsrv_fetch_array 函式一律會根據預設 PHP 資料類型傳回資料。 如需如何指定 PHP 資料類型的相關資訊,請參閱如何:指定 PHP 資料類型。
如果擷取沒有名稱的欄位,則陣列元素的關聯索引鍵會是空字串 ("")。 如需詳細資訊,請參閱 sqlsrv_fetch_array。
SQLSRV 驅動程式 API 參考
關於文件中的程式碼範例
Microsoft Drivers for PHP for SQL Server 的程式設計指南