Select php and bank connection- Beginner [closed]

0

I'm having a big problem .. I'm a beginner with Php, I've researched a lot but I can not go on.

You're giving this error

  Uncaught Error: Call to a member function mysqli_fetch_array () on boolean in C: \ xampp \ htdocs \ test.php **

Where is the error? Sorry if it's something simple ..

<?php
//Inicia página mudando a URL
    echo "<html>";
    echo "<head>";
    echo "    <title>Cliente</title>";
    echo "    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">";
    echo "</head>";
    echo "<body>";

    //Conexão com o Banco
    $servidor = '1.1';
    $usuario = '**';
    $senha = '**';
    $banco = 'Organizacoes';
    // Conecta-se ao banco de dados MySQL
    $mysqli = new mysqli($servidor, $usuario, $senha, $banco);
    // Caso algo tenha dado errado, exibe uma mensagem de erro
    if (mysqli_connect_errno()) trigger_error(mysqli_connect_error());  

    // Conecta ao banco
    $mysqli = new mysqli('1.1', '**', '**', '**');

    // Executa uma consulta
    $sql = "SELECT 'id', 'nome' FROM Organizacoes LIMIT 5";
    $query = $mysqli->query($sql);

    while ($dados = $query->mysqli_fetch_array()) {
    echo 'ID: ' . $dados['id'] . '';
    echo 'Nome: ' . $dados['nome'] . '';
}
    echo 'Registros encontrados: ' . $query->num_rows;

?>
    
asked by anonymous 04.05.2018 / 20:55

1 answer

1

The error is in this line:

 // Executa uma consulta
$sql = "SELECT 'id', 'nome' FROM 'Instituicoes' LIMIT 5";

The correct one would be

// Executa uma consulta
$sql = "SELECT 'id', 'nome' FROM Instituicoes LIMIT 5";

No quotation marks encapsulating the table name.

The error you mentioned says that the mysqli_fetch_array() function was trying to execute on boolean , that is, its $query variable failed and was returning false. From there, just check what might have failed her. It is often easier to verify this error if you test your query directly in the database and see what it returns you.

    
04.05.2018 / 20:59