My code gives this error Fatal error: Call to a member function fetch_assoc () on boolean

0

Does anyone know why this error in php when querying a table in mysql

  

"Fatal error: call fetch_assoc () on boolean in   /storage/emulated/0/site/roja/adm/editar_produto.php on line 85 "

Since in another page using the same code works normal, the code is below.In that same page I look for information to be edited.

$sql ="Select * from categorias";
$resultado = $con ->real_query($sql);
$resultado= $con->use_result();
while($row = $resultado->fetch_assoc()){
   echo $row["id"] ."$nbsp;";
   echo $row['nome'] ."<br>"; 
}
    
asked by anonymous 08.05.2018 / 21:11

1 answer

0

It is simple, $resultado= $con->use_result(); returned FALSE , so if $resultado has FALSE as value is not object and it is impossible to access ->fetch_assoc()

Then just treat with a IF and use $con->error to get the query error, which is what is causing the FALSE return of use_result

Example:

$resultado = $con->use_result();

if ($resultado) {
    while($row = $resultado->fetch_assoc()){
       echo $row["id"] ."$nbsp;";
       echo $row['nome'] ."<br>"; 
    }
} else {
    die($con->error);
}
    
08.05.2018 / 21:25