Error in consultation

Asked

Viewed 36 times

0

What is wrong?

I did everything right, researched and resumed the mistake:

Notice: Trying to get property 'num_rows' of non-object in C:\xampp\htdocs\tipo\consulta-online.php on line 21

What can it be?

<?php

    $servidorBanco = "localhost";
    $usuarioBanco = "root";
    $senhaBanco = "12345";
    $dadosBanco = "bancodedados";

    $conexao = mysqli_connect($servidorBanco, $usuarioBanco, $senhaBanco, $dadosBanco);

    if(!$conexao){
        die("Falha na conexao: " . mysqli_connect_error());
    }else{
        //echo "Conexao realizada com sucesso";
    }   

    $sql = "SELECT * FROM sis_clientes";
    $result = $conexao->query($sql);

    if ($result->num_rows > 0) {

       while($row = $result->fetch_assoc()) {

           //É isso que eu quero que consulta, quero que consulte 
           //todos os dados refere a esse campo da tabela "login"
           echo $row["login"];

        }
    } else {
        echo "0 results";
    }
    $conexao->close();
?>
  • Check if the table actually exists. If it exists, add a die($conexao->error); after $conexao->query.

  • Gave this error. Parse error: syntax error, Unexpected 'die' (T_EXIT) in C: xampp htdocs query-online.php on line 17

  • i did so. $result = $connected->query($sql) die($connected->error);

  • And tbm tried so. $result = $connected->query($sql); $result = $connected->error; Also no results.

  • Missed the or. The correct is $result = $conexao->query($sql) or die($conexao->error);

  • Show amigo, that was it. Problem was that in the database name was sis_clientes and was sis_cliente, thanks. My vote is yours. Put your answer I will add the vote as completed.

Show 1 more comment

1 answer

1


This error happens when there is an error in query.

The method query of extension MySQLi, can return 3 results:

  • false: In case of failure, for example, when the query is invalid;
  • MySQLi_Result: If successful for the darlings of the kind SELECT, SHOW, DESCRIBE or EXPLAIN;
  • true: If successful for the darlings other than those mentioned in the previous item, for example, insert and update.

When we use a query valid, however, with the table name or different fields, the method query returns us a false and so we can’t access the property as num_rows.

That is why it is important that we use verification with mysqli_error or $conn->error. It is obviously necessary to use this practice carefully. Ideally you store these values in a log and treat the message to the customer, for example:

<?php

$conexao = mysqli_connect("localhost:3307", "root", "123456", "teste");

$sql = "SHOW TABLE;"; /* Erro proposital */
$result = $conexao->query($sql);

if ($conexao->error) {
    Log::warning( $conexao->error );
    die("Não foi possível completar a operação. Entre em contato com um adminstrador do site.");
}

This will prevent malicious people from getting errors from their application. So only you, the developer, will know the errors and can fix them in your application.

Browser other questions tagged

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