分享方式:


sqlsrv_num_rows

下載 PHP 驅動程式

報告結果集內的資料列數目。

語法

  
sqlsrv_num_rows( resource $stmt )  

參數

$stmt:對其計算資料列的結果集。

傳回值

如果計算資料列數目時發生錯誤,則會傳回false 。 否則會傳回結果集內的資料列數目。

備註

sqlsrv_num_rows 需要用戶端、靜態或索引鍵集資料指標,如果您使用順向資料指標或動態資料指標,則會傳回 false。 (預設值為順向資料指標)。如需資料指標的詳細資訊,請參閱 sqlsrv_query資料指標類型 (SQLSRV 驅動程式)

範例

<?php  
   $server = "server_name";  
   $conn = sqlsrv_connect( $server, array( 'Database' => 'Northwind' ) );  
  
   $stmt = sqlsrv_query( $conn, "select * from orders where CustomerID = 'VINET'" , array(), array( "Scrollable" => SQLSRV_CURSOR_KEYSET ));  
  
   $row_count = sqlsrv_num_rows( $stmt );  
  
   if ($row_count === false)  
      echo "\nerror\n";  
   else if ($row_count >=0)  
      echo "\n$row_count\n";  
?>  

下列範例顯示若有一個以上的結果集 (批次查詢),則僅當您使用用戶端資料指標時資料列數目才適用。

<?php  
$serverName = "(local)";  
$connectionInfo = array("Database"=>"AdventureWorks");  
$conn = sqlsrv_connect( $serverName, $connectionInfo);  
  
$tsql = "select * from HumanResources.Department";  
  
// Client-side cursor and batch statements  
$tsql = "select top 2 * from HumanResources.Employee;Select top 3 * from HumanResources.EmployeeAddress";  
  
// works  
$stmt = sqlsrv_query($conn, $tsql, array(), array("Scrollable"=>"buffered"));  
  
// fails  
// $stmt = sqlsrv_query($conn, $tsql);  
// $stmt = sqlsrv_query($conn, $tsql, array(), array("Scrollable"=>"forward"));  
// $stmt = sqlsrv_query($conn, $tsql, array(), array("Scrollable"=>"static"));  
// $stmt = sqlsrv_query($conn, $tsql, array(), array("Scrollable"=>"keyset"));  
// $stmt = sqlsrv_query($conn, $tsql, array(), array("Scrollable"=>"dynamic"));  
  
$row_count = sqlsrv_num_rows( $stmt );  
echo "\nRow count for first result set = $row_count\n";  
  
sqlsrv_next_result($stmt);  
  
$row_count = sqlsrv_num_rows( $stmt );  
echo "\nRow count for second result set = $row_count\n";  
?>  

另請參閱

SQLSRV 驅動程式 API 參考