In PHP, a popular error called “mysqli_fetch_array() expects parameter 1 to be mysqli_result” is often encountered. This happens when the mysqli_fetch_array() method receives the parameter which is not a query result.
Let us understand a little more about the error and how it can be fixed.
Error Code
<?php
// Select data from table table_employee
$resultAll = mysqli_query($dbcon, "SELECT * FROM table_employee");
if(!$resultAll){
die(mysqli_error($dbcon));
}
// Check is result set le grater then 0
if (mysqli_num_rows($resultAll) > 0) {
while($rowCatData = mysqli_fetch_array($dbcon, $resultAll)){
echo $rowCatData["employee_name"].'<br>';
}
}
?>
Output
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result
In the above example, the error occurs when we pass the $dbcon string to the mysqli_fetch_array() function. This s because the mysqli_fetch_array() function takes a single parameter as a resultant object which comes from the mysqli_query() function. So, when it receives a connection string such as the $dbcon variable, it throws the error.
Thus, you do not have to pass the database connection object variable to the mysqli_fetch_array() function.
Correct Example:
<?php
// Select data from table table_employee
$resultAll = mysqli_query($dbcon, "SELECT * FROM table_employee");
if(!$resultAll){
die(mysqli_error($dbcon));
}
// Check is result set le grater then 0
if (mysqli_num_rows($resultAll) > 0) {
while($rowCatData = mysqli_fetch_array($resultAll)){
echo $rowCatData["employee_name"].'<br>';
}
}
?>
Error Code:
$rowCatData = mysqli_fetch_array($dbcon, $resultAll)
Correct Code:
$rowCatData = mysqli_fetch_array($resultAll)
Here, you can see that the $dbcon string is replaced with the $resultAll variable, and the code runs properly.