Warning: mysqli_fetch_assoc() expects Parameter 1 to be mysqli_result, Boolean Given in

Asked

Viewed 19,743 times

6

I’m having a simple problem when it comes time to take a dice from a table..

The mistake 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);

2 answers

5


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

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);
  • If I take those " it leaves the entire remainder of the code inexecutavel mano lolz. Just check your codebox, the bottom part gets the color red without the "

  • @Guilherme See if it works now. :)

  • It worked perfectly! Thanks bro S2

  • @Guilherme If it worked then, mark the answer as accepted, click on the mark just below the score, this helps future visitors with similar problems to know which answer worked for you. =)

  • I liked this black chicken avatar, tribal style. I just didn’t get what the Transformers logo is in the middle. Is it from any of the movies? I only watched the 1st (then delete the comment)

  • @Bacco Difficult to have seen only the 1st =) this is the logo of the Decepticons.

  • Then the "logo" of the Icons I know (the 1st "long" drawing of the Transformers I saw in the cinema, I’m getting old), I didn’t know this tribal thing around.

  • @Bacco A imagination is the limit. the/

  • In fact, in the English version of the film, Galvatron’s voice is from Leonard Nimoy

  • @Bacco I did not know this fact no.. researching further on this, he also made the voice of Sentinel in the Transformers: The Dark Side of the Moon.

Show 5 more comments

3

This error happens when your query returns an error, to debug it create a variable with sql print it and direct test in the database also activate displays the errors when running 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));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.