当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


PHP mysqli_stmt_num_rows()用法及代码示例



定义和用法

这个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_num_rows() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。