Warning: mysqli_fetch_assoc () expects parameter 1 to be mysqli_result, boolean given in [duplicate]

5

I have a simple problem when picking up a data from a table ..

The error is

  

Warning: mysqli_fetch_assoc () expects parameter 1 to be mysqli_result,   boolean given in

Code:

$sql = mysqli_query($conexao, "SELECT diasvip FROM login WHERE userid = ".$_POST['userid']."");
    while($exibe = mysqli_fetch_assoc($sql)){
        echo "<div class='alert alert-success' role='alert'>Dias Vip do Usuário: ".$exibe['diasvip']."</div>";  
    }
    mysqli_close($conexao);
    
asked by anonymous 07.03.2015 / 14:48

2 answers

4

This error is thrown because the query was not executed correctly, before entering While(..) {...} , check the result returned:

if($sql === FALSE) {
    // Consulta falhou, parar aqui 
    die(mysqli_error());
}

Your code should look like this:

$sql = mysqli_query($conexao, "SELECT diasvip FROM login WHERE userid = '".$_POST['userid']."'");
if($sql === FALSE) { 
   die(mysqli_error());
}

while($exibe = mysqli_fetch_assoc($sql)){
  echo "<div class='alert alert-success' role='alert'>Dias Vip do Usuário: ".$exibe['diasvip']."</div>";  
}
mysqli_close($conexao);
    
07.03.2015 / 14:58
3

This error happens when your query returns an error, to debug it, create a variable with the sql print it, and test it directly on the database, also enable errors when executing the query with mysqli_error

$str_consulta = "SELECT diasvip FROM login WHERE userid = ".$_POST['userid']);
echo $str_consulta;
$sql = mysqli_query($conexao, $str_consulta)  or die(mysqli_error($db));
    
07.03.2015 / 15:17