©
이 문서에서는 PHP 중국어 웹사이트 매뉴얼 풀어 주다
(No version information available, might only be in Git)
sqlsrv_num_rows — Retrieves the number of rows in a result set
$stmt
)Retrieves the number of rows in a result set. This function requires that the statment resource be created with a static or keyset cursor. For more information, see sqlsrv_query() , sqlsrv_prepare() , or » Specifying a Cursor Type and Selecting Rows in the Microsoft SQLSRV documentation.
stmt
The statement for which the row count is returned. The statment resource must be created with a static or keyset cursor. For more information, see sqlsrv_query() , sqlsrv_prepare() , or » Specifying a Cursor Type and Selecting Rows in the Microsoft SQLSRV documentation.
Returns the number of rows retrieved on success and FALSE
if an error occurred.
If a forward cursor (the default) or dynamic cursor is used, FALSE
is returned.
Example #1 sqlsrv_num_rows() example
<?php
$server = "serverName\sqlexpress" ;
$connectionInfo = array( "Database" => "dbName" , "UID" => "username" , "PWD" => "password" );
$conn = sqlsrv_connect ( $server , $connectionInfo );
$sql = "SELECT * FROM Table_1" ;
$params = array();
$options = array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
$stmt = sqlsrv_query ( $conn , $sql , $params , $options );
$row_count = sqlsrv_num_rows ( $stmt );
if ( $row_count === false )
echo "Error in retrieveing row count." ;
else
echo $row_count ;
?>
[#1] wazz3r at gmail dot com [2013-01-21 14:04:38]
Try to avoid using this function if you need good performance. Specifying "Scrollable" in the options will make you queries take ages to run. If your result contains less than 5000 rows (might vary on different hardware) its faster to not use "Scrollable" and loop over them in php instead.
If you need to check if a result contains rows use "sqlsrv_has_rows()", this function works without "Scrollable". After removing all my "Scrollable" queries, my page loadtime went from 900ms to 60ms.
To demonstrate, here is a query that returns 100 rows:
<?php
for($i = 0; $i < 100; $i++) {
$q = "SELECT sku,name FROM product WHERE visible = 1";
$result = sqlsrv_query($db,$q,array(), array( "Scrollable" => SQLSRV_CURSOR_KEYSET ));
while($row = sqlsrv_fetch_array($result)) {}
}
?>
This takes about 10s! Thats 10 qps..
Now if we remove "Scrollable":
<?php
for($i = 0; $i < 100; $i++) {
$q = "SELECT sku,name FROM product WHERE visible = 1";
$result = sqlsrv_query($db,$q);
while($row = sqlsrv_fetch_array($result)) {}
}
?>
This will run in 300ms, about 334 qps!
[#2] smhahmadi [2012-12-30 09:50:16]
Note that when migrating your MS SQL Server PHP Driver from MSSQL to SQLSRV, if you have used mssql_num_rows, replacing them with sqlsrv_num_rows and replacing mssql_query($query, $mssql_link) with sqlsrv_query($sqlsrv_link, $query) calls will make your sqlsrv_num_rows calls fail. In order to avoid that, you should specify either static, keyset or buffered cursors (buffered cursor has been available since SQLSRV 3.0) when calling sqlsrv_query. For example,
<?php
mssql_query($query, $mssql_link);
// is equivalent to
sqlsrv_query($sqlsrv_link, $query, array(), array('Scrollable' => 'buffered'));
?>
Using the buffered cursor is "more equivalent" than using the static or keyset cursors to simple mssql_query calls, since it caches the entire result set in client memory.