[SOLUTION] Warning: Mysqli_num_rows() Expects Parameter 1 to Be Mysqli_result

Mysqli_num_rows() expects parameter 1 to be mysqli_result is one of the most basic kinds of errors that occur in PHP while using MySQLi functions.

Warning: Mysqli_num_rows() Expects Parameter 1 to Be Mysqli_result

If you are getting this error, don’t panic; there is nothing to worry about because it is not an error actually. Basically, “mysqli_num_rows() expects parameter 1 to be mysqli_result” is a warning which occurs while fetching data using MySQLi functions.

When we use the mysqli_query() function with the SELECT query, it returns some resultant data from the database. We can use mysqli_fetch_array() or mysqli_fetch_assoc() function to fetch every resultant row.

Example

We have a table of students with two columns, “id” and “name.”

Warning: Mysqli_num_rows() Expects Parameter 1 to Be Mysqli_result

and we have a PHP code to fetch the student name as below.

<?php
    $conn = mysqli_connect("localhost", "USER_NAME", "PASSWORD", "DATABASE_NAME");
    $result = mysqli_query($conn,"SELECT * FROM `students` WHERE `id`=4");
    while($data = mysqli_fetch_assoc($result)){
        echo $data["name"];
    }
?>

If we want to find the name of the student with id ‘1’, it will display the output as below.

Output

Nick

The above PHP code is correct because there is a field with ‘id’ = 1

Now, if we try to fetch the name where ‘id’ = 4

Here the problem occurs of ‘Warning: Mysqli_num_rows() Expects Parameter 1 to Be Mysqli_result‘.

This warning might appear on your PHP page or in the error.log file; it depends on your working environment.

Solution

As we can understand from the above explanation that mysqli_query() returns no data because there is no data record exist with ‘id’ = 4 but still program proceeds and runs the next query of mysqli_fetch_assoc(), which expects at least one row in $result variable to fetch data and it prompts a warning.

To resolve that problem, we just have to check the number of results and only fetch data if there is at least 1 row in resultant data.

SOLVED CODE

<?php
    $conn = mysqli_connect("localhost", "USER_NAME", "PASSWORD", "DATABASE_NAME");
    $result = mysqli_query($conn,"SELECT * FROM `students` WHERE `id`=4");
    if(mysqli_num_rows($result) > 0){
        while($data = mysqli_fetch_assoc($result)){
            echo $data["name"];
        }
    }else{
         echo "No Records Found!";
    }
?>

Read Also: Mysqli_fetch_assoc vs Mysqli_fetch_array [With Example]

Conclusion

I hope now you can resolve the mentioned error in your amazing PHP project.

Happy Coding 🙂

5 thoughts on “[SOLUTION] Warning: Mysqli_num_rows() Expects Parameter 1 to Be Mysqli_result

  1. i can’t solve this error please help me..

    error = Fatal error: Uncaught TypeError: mysqli_num_rows(): Argument #1 ($result) must be of type mysqli_result, bool given

    1. Hi there,

      It says the problem is in your argument that you give in mysqli_num_rows($result).
      Your $result variable should be of type mysqli_result but it is Boolean type.

      Make sure you are having your mysqli_result in the correct variable and pass it to mysqli_num_rows().

      Example:
      $result = mysqli_query($con, SQL_QUERY_HERE);
      $row_count = mysqli_num_rows($result);

      I hope this will help 🙂

      1. hello…this is my code but again one time it will throw me error..

        error = Fatal error: Uncaught ArgumentCountError: mysqli_query() expects at least 2 arguments, 1 given in C:\xampp\htdocs\FOURM\parl\log1.php:10 Stack trace: #0 C:\xampp\htdocs\FOURM\parl\log1.php(10): mysqli_query(false) #1 {main} thrown in C:\xampp\htdocs\FOURM\parl\log1.php on line 10

        how can i solve this?

        1. You are passing a single argument in your mysqli_query() method.
          But, mysqli_query() requires 2 arguments which are:
          1. MySQL Connection variable
          2. MySQL Query string

          In my above answer I have written:

          $result = mysqli_query($con, SQL_QUERY_HERE);

          Where $con is my connection variable and SQL_QUERY_HERE represents SQL query string.

Leave a Reply

Your email address will not be published.