mysqli_num_rows expects parameter 1
Let's first grasp the definition of mysqli_num_rows before going on to the actual reason and possible solution for the current problem. It will assist us in identifying where we are making errors, particularly for beginners.
Syntax
mysqli_num_rows ($resultSet);
Input param:: $resultSet is a required parameter.
Return: Total number of rows in resultSet.
Based on the definition we are required to pass resultset returned from mysqli_query or other similar result set retrieval functions as arguments.
Now let's take a look at the technique below for a step-by-step guide and see how we can fix mysqli_num_rows expects parameter 1 warning with code examples.
Scroll for More Useful Information and Relevant FAQs
Why does getting mysqli_num_rows() expect parameter 1 to be mysqli_result, boolean given, and how to fix it?
Here we are reproducing errors for demonstration purposes. Let's go through the following code and understand the reason.
<?php
$host = "localhost"; // Specify your server Host/IP address
$uname = "uname"; // Replace your Database username here
$pwd = "password"; // Replace your Database password here
$db= "demo"; //Replace your database name here
$mysqli = new mysqli($host,$uname,$pwd,$db);
// Check connection
if ($mysqli -> connect_errno) {
echo "Failed to connect Database: " . $mysqli -> connect_error;
exit();
}
$resultSet = mysqli_query($mysqli, "SELECT name, email FROM tbl_notExist");
var_dump($resultSet);
var_dump(mysqli_num_rows($resultSet));
Result:
bool(false)
NULL
Warning : PHP Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given
Explanation:
- Here we are trying to execute a query against a non-existing database table. Therefore as the table does not exist mysqli_query returns a false boolean value.
- But as per definitions described above it required the result set as input param but false given and so it raised a warning.
- Therefore in most cases, we have an issue in the query specified which is wrong and requires fixing the query.
What if rewrite above program using the correct table
$resultSet = mysqli_query($mysqli, "SELECT name, email FROM tbl_employee");
var_dump($resultSet);
var_dump(mysqli_num_rows($resultSet));
Result:
object(mysqli_result)#2 (5) { ["current_field"]=> int(0) ["field_count"]=> int(2) ["lengths"]=> NULL ["num_rows"]=> int(1) ["type"]=> int(0) }
1
Explanation:
As we can see mysqli_query returns mysqli_result reference with correct query and returns a count of rows returned as 1.
Why does the mysqli_num_rows function return an unexpected count?
There can be common reasons behind this problem. Would like to highlight again the definition mysqli num rows function that returns rows counts from the result set of a specified MySQL query.
Therefore if we perform any MySQL Aggregate Functions query such as count., AVG(), min, max, etc will return one single row meaning that the result set contains only one record and hence num rows functions return 1 in such cases.
It is recommended to run a specified query using any MySQL Tools such as phpMyAdmin, MySQL Workbench, HeidiSQL, and others to make sure the query is correct and verify the number of records returned by it.