PHP error with Mysql

Asked

Viewed 42 times

0

I’m making a mistake, and I don’t understand why. The mistake:

Fatal error: Call to a member function fetch_assoc() on boolean

the excerpt from the error:

require 'conecta.php';
    $sql = "SELECT `nomep`, `endereco` FROM `cadastropn` WHERE `cidade` = Caxias do Sul";
    $result = mysqli_query($mysqli, $sql);
    while($linha = $result->fetch_assoc()) {
        echo '<article>' . $linha['nomep'];
        $nome = $linha['nomep'];
        echo '</article>';
    }

The strange thing is that the mistake only happens when I put the WHEREcity= Caxias do Sul";

  • 1

    Ahh, yes now I’ve seen the bug and already corrected. Thank you friend!

  • 1

    A if( !$result ) die( mysqly_error( $mysqli ) ); is sufficient to test for errors, but does not use the code in production. More details here: http://answall.com/a/141797/70

1 answer

3


The mistake is because mysqli_query FALSE, so in error appears BOLEAN (that is not the type expected to fetch_assoc).

The reason is that Caxias do Sul is a string and has it in quotes. It should look like this: 'Caxias do Sul'.

Altere of:

$sql = "SELECT `nomep`, `endereco` FROM `cadastropn` WHERE `cidade` = Caxias do Sul";

To:

 $sql = "SELECT `nomep`, `endereco` FROM `cadastropn` WHERE `cidade` = 'Caxias do Sul'";

Browser other questions tagged

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