使用游标进行PHP SQLSRV查询的方法与注意事项
目录
- 游标类型
- 选择结果集中的行
- SQLSRV Driver之服务端游标
- SQLSRV Driver之客户端游标
SQLSRV驱动程序允许您创建一个结果集,其中包含可以根据游标类型以任何顺序访问的行。本主题将讨论客户端(缓冲)和服务器端(非缓冲)游标及其用法。
使用SQLSRV查询时,默认情况下,使用向前游标,它允许您从结果集的第一行开始一次移动一行,直到到达结果集的末尾。当然,有时这不是你所期待的,这就需要指定游标类型。
游标类型
您可以使用Scrollable来指定结果集的游标,以实现各种以顺序访问结果集中的任何行。下表列出了可以传递给Scrollable的选项。
如果查询生成多个结果集,则Scrollable选项应用于所有结果集。
选择结果集中的行
创建结果集后,就可以使用sqlsrv_fetch、sqlsrv_fetch_array或sqlsrv_fetch_object来指定行了。
下表说明了可以对行进行的操作。
SQLSRV Driver之服务端游标
以下示例显示了各种游标的效果。在示例的第33行,您可以看到指定不同游标的三条查询语句中的第一条。其中两个查询语句被注释。每次运行程序时,使用不同的游标类型查看第47行上的数据库更新效果。
<?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之客户端游标
客户端游标是Microsoft Drivers for PHP for SQL Server 3.0版中添加的一项功能,它允许您在内存中缓存整个结果集。使用客户端游标执行查询后,行计数可用。
客户端游标应该用于中小型结果集。对大型结果集使用服务器端游标。
如果缓冲区不够大,无法容纳整个结果集,则查询将返回false。您可以将缓冲区大小增加到PHP内存限制。
使用SQLSRV驱动程序,可以使用sqlsrv_configure来设置ClientBufferMaxKBSize以配置保存结果集的缓冲区的大小。
sqlsrv_get_config返回ClientBufferMaxKBSize的值。
您还可以在php.ini文件中使用sqlsrv.ClientBufferMaxKBSize来设置最大缓冲区大小(例如,sqlsrv.clientBufferMaxKBSign=1024)。
以下示例显示:
1、行计数始终可用于客户端游标。
2、使用客户端游标和批处理语句。
<?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)并配置了不同的客户端缓冲区大小。
<?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"; } ?>
到此这篇关于使用游标进行PHP SQLSRV查询的方法与注意事项的文章就介绍到这了,更多相关PHP SQLSRV游标查询内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!