PHP error with MySQL [closed]

0

I have an error and I do not understand why. The error:

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

the error snippet:

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 error only happens when I put WHERE city = Caxias do Sul";

    
asked by anonymous 02.09.2016 / 00:08

1 answer

3

The error is because mysqli_query FALSE , so the error appears BOLEAN (which is not the expected type for fetch_assoc ).

The cause is that Caxias do Sul is a string and has to be quoted. It should look like this: 'Caxias do Sul' .

Change from:

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

To:

 $sql = "SELECT 'nomep', 'endereco' FROM 'cadastropn' WHERE 'cidade' = 'Caxias do Sul'";
    
02.09.2016 / 00:30