While working with PHP and MySQL, we come across an error called mysqli_num_rows() expects parameter 1 to be mysqli_result. We will take a closer look at this error in this article.
Error Code 1
$dbcon = mysqli_connect("localhost", "root", "", "databasename");
if (!$dbcon) {
die("Connection failed: " . mysqli_connect_error());
}
$resultAll = mysqli_query($dbcon, "SELECT * FROM employee");
if(!$resultAll){
die(mysqli_error($dbcon));
}
if (mysqli_num_rows($resultAllNEW) > 0) {
while($rowCatData = mysqli_fetch_array($resultAll)){
echo $rowCatData["employee_name"].'<br>';
}
}
Output:
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result
Explanation
In the code, you can check that the result stored in the variable $resultAll is not passed correctly. It is necessary that you assign the result properly to this variable before passing it to the mysqli_num_rows method.
Error Code 2
mysqli_num_rows($resultAllNew)
Correct Code
mysqli_num_rows($resultAll)
Explanation
The code above runs perfectly as we have stored the result in the variable $resultAll.
Correct Code
$dbcon = mysqli_connect("localhost", "root", "", "databasename");
if (!$dbcon) {
die("Connection failed: " . mysqli_connect_error());
}
$resultAll = mysqli_query($dbcon, "SELECT * FROM employee");
if(!$resultAll){
die(mysqli_error($dbcon));
}
if (mysqli_num_rows($resultAll) > 0) {
while($rowCatData = mysqli_fetch_array($resultAll)){
echo $rowCatData["employee_name"].'<br>';
}
}