Register Login

What is 'mysqli_fetch_assoc() expects parameter 1 to be mysqli_result' Error?

28 Jul 2020 12:09 pm || 0

The mysqli_fetch_assoc() expects parameter 1 to be mysqli_result error is common in PHP programming. It is raised when the mysqli_fetch_assoc() method receives an incorrect parameter.    

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_assoc($dbcon, $resultAll)){
  		echo $rowCatData["employee_name"].'<br>';
	}
}
?>

Output

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result

The program here throws the occur when the $dbcon string is passed to the mysqli_fetch_assoc() function. The mysqli_fetch_assoc() function takes a single parameter which is the result of the object that comes from the mysqli_query() function.

So, when you pass the database connection object variable $dbcon, the error is raised.

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_assoc($resultAll)){
  		echo $rowCatData["employee_name"].'<br>';
	}
}
?>

Error Code:

$rowCatData = mysqli_fetch_assoc($dbcon, $resultAll)

Correct Code:

$rowCatData = mysqli_fetch_assoc($resultAll)

Here, you can see that when the $dbcon variable is removed and replaced with the $resultAll variable as the parameter for mysqli_fetch_assoc() method, the error is prevented.