使用游標(biāo)進(jìn)行PHP SQLSRV查詢的方法與注意事項(xiàng)
目錄
- 游標(biāo)類型
- 選擇結(jié)果集中的行
- SQLSRV Driver之服務(wù)端游標(biāo)
- SQLSRV Driver之客戶端游標(biāo)
SQLSRV驅(qū)動程序允許您創(chuàng)建一個(gè)結(jié)果集,其中包含可以根據(jù)游標(biāo)類型以任何順序訪問的行。本主題將討論客戶端(緩沖)和服務(wù)器端(非緩沖)游標(biāo)及其用法。
使用SQLSRV查詢時(shí),默認(rèn)情況下,使用向前游標(biāo),它允許您從結(jié)果集的第一行開始一次移動一行,直到到達(dá)結(jié)果集的末尾。當(dāng)然,有時(shí)這不是你所期待的,這就需要指定游標(biāo)類型。
游標(biāo)類型
您可以使用Scrollable來指定結(jié)果集的游標(biāo),以實(shí)現(xiàn)各種以順序訪問結(jié)果集中的任何行。下表列出了可以傳遞給Scrollable的選項(xiàng)。
如果查詢生成多個(gè)結(jié)果集,則Scrollable選項(xiàng)應(yīng)用于所有結(jié)果集。
選擇結(jié)果集中的行
創(chuàng)建結(jié)果集后,就可以使用sqlsrv_fetch、sqlsrv_fetch_array或sqlsrv_fetch_object來指定行了。
下表說明了可以對行進(jìn)行的操作。
SQLSRV Driver之服務(wù)端游標(biāo)
以下示例顯示了各種游標(biāo)的效果。在示例的第33行,您可以看到指定不同游標(biāo)的三條查詢語句中的第一條。其中兩個(gè)查詢語句被注釋。每次運(yùn)行程序時(shí),使用不同的游標(biāo)類型查看第47行上的數(shù)據(jù)庫更新效果。
<?php $server = "server_name"; $conn = sqlsrv_connect( $server, array( "Database" => "test" )); if ( $conn === false ) { die( print_r( sqlsrv_errors(), true )); } $stmt = sqlsrv_query( $conn, "DROP TABLE dbo.ScrollTest" ); if ( $stmt !== false ) { sqlsrv_free_stmt( $stmt ); } $stmt = sqlsrv_query( $conn, "CREATE TABLE ScrollTest (id int, value char(10))" ); if ( $stmt === false ) { die( print_r( sqlsrv_errors(), true )); } $stmt = sqlsrv_query( $conn, "INSERT INTO ScrollTest (id, value) VALUES(?,?)", array( 1, "Row 1" )); if ( $stmt === false ) { die( print_r( sqlsrv_errors(), true )); } $stmt = sqlsrv_query( $conn, "INSERT INTO ScrollTest (id, value) VALUES(?,?)", array( 2, "Row 2" )); if ( $stmt === false ) { die( print_r( sqlsrv_errors(), true )); } $stmt = sqlsrv_query( $conn, "INSERT INTO ScrollTest (id, value) VALUES(?,?)", array( 3, "Row 3" )); if ( $stmt === false ) { die( print_r( sqlsrv_errors(), true )); } $stmt = sqlsrv_query( $conn, "SELECT * FROM ScrollTest", array(), array( "Scrollable" => "keyset" )); // $stmt = sqlsrv_query( $conn, "SELECT * FROM ScrollTest", array(), array( "Scrollable" => "dynamic" )); // $stmt = sqlsrv_query( $conn, "SELECT * FROM ScrollTest", array(), array( "Scrollable" => "static" )); $rows = sqlsrv_has_rows( $stmt ); if ( $rows != true ) { die( "Should have rows" ); } $result = sqlsrv_fetch( $stmt, SQLSRV_SCROLL_LAST ); $field1 = sqlsrv_get_field( $stmt, 0 ); $field2 = sqlsrv_get_field( $stmt, 1 ); echo "\n$field1 $field2\n"; $stmt2 = sqlsrv_query( $conn, "delete from ScrollTest where id = 3" ); // or // $stmt2 = sqlsrv_query( $conn, "UPDATE ScrollTest SET id = 4 WHERE id = 3" ); if ( $stmt2 !== false ) { sqlsrv_free_stmt( $stmt2 ); } $result = sqlsrv_fetch( $stmt, SQLSRV_SCROLL_LAST ); $field1 = sqlsrv_get_field( $stmt, 0 ); $field2 = sqlsrv_get_field( $stmt, 1 ); echo "\n$field1 $field2\n"; sqlsrv_free_stmt( $stmt ); sqlsrv_close( $conn ); ?>
SQLSRV Driver之客戶端游標(biāo)
客戶端游標(biāo)是Microsoft Drivers for PHP for SQL Server 3.0版中添加的一項(xiàng)功能,它允許您在內(nèi)存中緩存整個(gè)結(jié)果集。使用客戶端游標(biāo)執(zhí)行查詢后,行計(jì)數(shù)可用。
客戶端游標(biāo)應(yīng)該用于中小型結(jié)果集。對大型結(jié)果集使用服務(wù)器端游標(biāo)。
如果緩沖區(qū)不夠大,無法容納整個(gè)結(jié)果集,則查詢將返回false。您可以將緩沖區(qū)大小增加到PHP內(nèi)存限制。
使用SQLSRV驅(qū)動程序,可以使用sqlsrv_configure來設(shè)置ClientBufferMaxKBSize以配置保存結(jié)果集的緩沖區(qū)的大小。
sqlsrv_get_config返回ClientBufferMaxKBSize的值。
您還可以在php.ini文件中使用sqlsrv.ClientBufferMaxKBSize來設(shè)置最大緩沖區(qū)大小(例如,sqlsrv.clientBufferMaxKBSign=1024)。
以下示例顯示:
1、行計(jì)數(shù)始終可用于客戶端游標(biāo)。
2、使用客戶端游標(biāo)和批處理語句。
<?php $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)); } $tsql = "select * from HumanResources.Department"; // Execute the query with client-side cursor. $stmt = sqlsrv_query($conn, $tsql, array(), array("Scrollable"=>"buffered")); if (! $stmt) { echo "Error in statement execution.\n"; die( print_r( sqlsrv_errors(), true)); } // row count is always available with a client-side cursor $row_count = sqlsrv_num_rows( $stmt ); echo "\nRow count = $row_count\n"; // Move to a specific row in the result set. $row = sqlsrv_fetch($stmt, SQLSRV_SCROLL_FIRST); $EmployeeID = sqlsrv_get_field( $stmt, 0); echo "Employee ID = $EmployeeID \n"; // Client-side cursor and batch statements $tsql = "select top 2 * from HumanResources.Employee;Select top 3 * from HumanResources.EmployeeAddress"; $stmt = sqlsrv_query($conn, $tsql, array(), array("Scrollable"=>"buffered")); if (! $stmt) { echo "Error in statement execution.\n"; die( print_r( sqlsrv_errors(), true)); } $row_count = sqlsrv_num_rows( $stmt ); echo "\nRow count for first result set = $row_count\n"; $row = sqlsrv_fetch($stmt, SQLSRV_SCROLL_FIRST); $EmployeeID = sqlsrv_get_field( $stmt, 0); echo "Employee ID = $EmployeeID \n"; sqlsrv_next_result($stmt); $row_count = sqlsrv_num_rows( $stmt ); echo "\nRow count for second result set = $row_count\n"; $row = sqlsrv_fetch($stmt, SQLSRV_SCROLL_LAST); $EmployeeID = sqlsrv_get_field( $stmt, 0); echo "Employee ID = $EmployeeID \n"; ?>
以下示例顯示了使用sqlsrv_prepare(sqlsrv_prepare)并配置了不同的客戶端緩沖區(qū)大小。
<?php $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)); } $tsql = "select * from HumanResources.Employee"; $stmt = sqlsrv_prepare( $conn, $tsql, array(), array("Scrollable" => SQLSRV_CURSOR_CLIENT_BUFFERED, "ClientBufferMaxKBSize" => 51200)); if (! $stmt ) { echo "Statement could not be prepared.\n"; die( print_r( sqlsrv_errors(), true)); } sqlsrv_execute( $stmt); $row_count = sqlsrv_num_rows( $stmt ); if ($row_count) echo "\nRow count = $row_count\n"; $row = sqlsrv_fetch($stmt, SQLSRV_SCROLL_FIRST); if ($row ) { $EmployeeID = sqlsrv_get_field( $stmt, 0); echo "Employee ID = $EmployeeID \n"; } ?>
到此這篇關(guān)于使用游標(biāo)進(jìn)行PHP SQLSRV查詢的方法與注意事項(xiàng)的文章就介紹到這了,更多相關(guān)PHP SQLSRV游標(biāo)查詢內(nèi)容請搜索以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持!
