定義和用法
這個mysqli_stmt_num_rows()函數接受一個語句對象作為參數,並返回給定語句結果集中的行數。
用法
mysqli_stmt_num_rows($stmt)
參數
Sr.No | 參數及說明 |
---|---|
1 |
stmt(Mandatory) 這是一個對象,表示執行 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 mysqli_stmt_affected_rows()用法及代碼示例
- PHP mysqli_stmt_fetch()用法及代碼示例
- PHP mysqli_stmt_data_seek()用法及代碼示例
- PHP mysqli_stmt_free_result()用法及代碼示例
- PHP mysqli_stmt_error()用法及代碼示例
- PHP mysqli_stmt_field_count()用法及代碼示例
- PHP mysqli_stmt_errno()用法及代碼示例
- PHP mysqli_stmt_get_result()用法及代碼示例
- PHP mysqli_stmt_init()用法及代碼示例
- PHP mysqli_stmt_bind_result()用法及代碼示例
- PHP mysqli_stmt_store_result()用法及代碼示例
- PHP mysqli_stmt_reset()用法及代碼示例
- PHP mysqli_stmt_attr_get()用法及代碼示例
- PHP mysqli_stmt_param_count()用法及代碼示例
- PHP mysqli_stmt_attr_set()用法及代碼示例
- PHP mysqli_stmt_prepare()用法及代碼示例
- PHP mysqli_stmt_sqlstate()用法及代碼示例
- PHP mysqli_stmt_bind_param()用法及代碼示例
- PHP mysqli_stmt_close()用法及代碼示例
- PHP mysqli_stmt_result_metadata()用法及代碼示例
注:本文由純淨天空篩選整理自 PHP mysqli_stmt_num_rows() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。