Fonction PHP mysqli_stmt_num_rows ()

Définition et utilisation

le mysqli_stmt_num_rows() La fonction accepte un objet instruction comme paramètre et renvoie le nombre de lignes dans le jeu de résultats de l'instruction donnée.

Syntaxe

mysqli_stmt_num_rows($stmt)

Paramètres

Sr. Non Paramètre et description
1

stmt(Mandatory)

Il s'agit d'un objet représentant une instruction exécutant une requête SQL.

Valeurs de retour

La fonction PHP mysqli_stmt_num_rows () renvoie une valeur entière indiquant le nombre de lignes dans le jeu de résultats renvoyé par l'instruction.

Version PHP

Cette fonction a été introduite pour la première fois dans la version 5 de PHP et fonctionne dans toutes les versions ultérieures.

Exemple

L'exemple suivant montre l'utilisation de la fonction mysqli_stmt_num_rows () (dans le style procédural) -

<?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);
?>

Cela produira le résultat suivant -

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

Exemple

Dans le style orienté objet, la syntaxe de cette fonction est $ con> num_rows; Voici l'exemple de cette fonction dans le style orienté objet $ 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();
?>

Cela produira le résultat suivant -

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

Exemple

Supposons que nous ayons créé une table nommée cricketers avec les données suivantes $ 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)

Si vous essayez d'appeler cette fonction directement, puisque les résultats ne sont pas encore stockés, elle renvoie 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);
?>

Cela produira le résultat suivant -

Number of rows in the table: 0