PHP mysqli_stmt_num_rows() 函式



定義和用法

mysqli_stmt_num_rows() 函式接受一個語句物件作為引數,並返回給定語句的結果集中的行數。

語法

mysqli_stmt_num_rows($stmt)

引數

序號 引數及說明
1

stmt(必填)

這是一個表示執行 SQL 查詢的語句的物件。

返回值

PHP mysqli_stmt_num_rows() 函式返回一個整數值,指示語句返回的結果集中包含的行數。

PHP 版本

此函式首次在 PHP 5 版本中引入,並在所有後續版本中均有效。

示例

以下示例演示了 mysqli_stmt_num_rows() 函式的用法(過程式風格)−

<?php
   $con = mysqli_connect("localhost", "root", "password", "mydb");

   mysqli_query($con, "CREATE TABLE Test(Name VARCHAR(255), AGE INT)");
   print("Table Created.....\n");
   mysqli_query($con, "insert into Test values('Raju', 25),('Rahman', 30),('Sarmista', 27)");
   print("Records Inserted.....\n");

   //Reading records
   $stmt = mysqli_prepare($con, "SELECT * FROM Test");

   //Executing the statement
   mysqli_stmt_execute($stmt);

   mysqli_stmt_store_result($stmt);

   //Number of rows
   $count = mysqli_stmt_num_rows($stmt);

   print("Number of rows in the table: ".$count."\n");

   //Closing the statement
   mysqli_stmt_close($stmt);

   //Closing the connection
   mysqli_close($con);
?>

這將產生以下結果−

Table Created.....
Records Inserted.....
Number of rows in the table: 3

示例

在面向物件風格中,此函式的語法為 $con->num_rows; 以下是此函式在面向物件風格中的示例 $minus;

<?php
   //Creating a connection
   $con = new mysqli("localhost", "root", "password", "mydb");

   $con -> query("CREATE TABLE Test(Name VARCHAR(255), AGE INT)");
   print("Table Created.....\n");
   $con -> query("insert into Test values('Raju', 25),('Rahman', 30),('Sarmista', 27)");
   print("Records Inserted.....\n");

   $stmt = $con -> prepare( "SELECT * FROM Test");

   //Executing the statement
   $stmt->execute();

   $stmt->store_result();

   //Number of rows
   $count = $stmt ->num_rows;
   print("Rows affected ".$count);

   //Closing the statement
   $stmt->close();

   //Closing the connection
   $con->close();
?>

這將產生以下結果−

Table Created.....
Records Inserted.....
Number of rows in the table: 3

示例

假設我們建立了一個名為 cricketers 的表,其中包含以下資料 $minus;

mysql> select * from cricketers;
+----+------------+------------+---------------+----------------+
| ID | First_Name | Last_Name  | Date_Of_Birth | Place_Of_Birth |
+----+------------+------------+---------------+----------------+
|  1 | Shikhar    | Dhawan     | 1981-12-05    | Delhi          |
|  2 | Jonathan   | Trott      | 1981-04-22    | CapeTown       | 
|  3 | Kumara     | Sangakkara | 1977-10-27    | Matale         |
|  4 | Virat      | Kohli      | 1988-11-05    | Delhi          |
|  5 | Rohit      | Sharma     | 1987-04-30    | Nagpur         |
|  6 | Ravindra   | Jadeja     | 1988-12-06    | Nagpur         |
+----+------------+------------+---------------+----------------+
6 rows in set (0.07 sec)

如果您嘗試直接呼叫此函式,由於結果尚未儲存,它將返回 0

<?php
   $con = mysqli_connect("localhost", "root", "password", "mydb");

   //Reading records
   $stmt = mysqli_prepare($con, "SELECT * FROM cricketers");

   //Executing the statement
   mysqli_stmt_execute($stmt);

   print("Number of rows in the table: ".mysqli_stmt_num_rows($stmt));

   //Closing the statement
   mysqli_stmt_close($stmt);

   //Closing the connection
   mysqli_close($con);
?>

這將產生以下結果−

Number of rows in the table: 0
php_function_reference.htm
廣告

© . All rights reserved.