Select php and connection to the Beginner database

Asked

Viewed 106 times

0

I have a big problem.. I’m a beginner with Php, I’ve researched a lot but I can’t go on.

You’re making that mistake

Uncaught Error: Call to a Member Function mysqli_fetch_array() on Boolean in C: xampp htdocs teste.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;

?>
  • Your query failed, has syntax error, in table names or fields if they go quote, simply remove them.

  • Remove the quotes in the while part?

  • 1

    SELECT 'id', 'nome' FROM 'Instituicoes' LIMIT 5, should just stay: SELECT id, nome FROM Instituicoes LIMIT 5

1 answer

1

The error is on this line:

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

The right thing would be

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

No quotes encapsulating table name.

The error you mentioned says the function mysqli_fetch_array() was trying to be executed on boolean, or its variable $query failed and was returning false. From there just check what may have failed it. Often, it is easier to check this error if you test your query directly in the database and see what it returns to you.

  • 2

    I suggest removing the quotation marks including the name of the fields SELECT id,nome FROM Instituicoes LIMIT 5

  • 1

    I don’t really know what the implication of leaving with or without single quotes in column names during SELECT is. In the database I use Postgresql, there is no problem in leaving simple quotes encapsulating column names.

  • 1

    Thank you so much for the guidelines, really.. I’m using Workbench and that select ran normally. Fixed the quotes in the table and gave this error: Fatal error: Uncaught Error: Call to Undefined method mysqli_result::mysqli_fetch_array() in C: xampp htdocs teste.php:31 Stack trace: #0 {main} thrown in C: xampp htdocs teste.php on line 31 Is there something wrong with this fetch_array?

  • So I withdraw my comment, in my systems I do not use quotation marks in these cases, when I used was error, but keep the code in a way that works

  • 1

    If you leave simple quotes in a column, the result of your query will have a column with the value that is between the quotes. In postgres the double Apas serve as an escape if the name has accents or other characters the equivalent in Mysql would be the crase.

Browser other questions tagged

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