Problem in consultation

Asked

Viewed 34 times

0

You are giving the error Warning: mysqli_num_rows() expects Parameter 1 to be mysqli_result, Boolean Given in. Can anyone help me?

$mes = date('m');
$ano = date('Y');

$sql = mysqli_query($conn, "SELECT * FROM table WHERE empresa='empresa' AND MONTH(data_atendimento) = '$mes' AND YEAR(data_atendimento) = '$ano'");
$numRegistros = mysqli_num_rows($sql);
  • Hello, I believe the error is in the query possibly in one of the parameters after Where tries to remove these parameters and run the query again to see if it will run normally and if you add some parameter in Where and the error occurs can identify which one is wrong inserted.

1 answer

1


William, there may be some error in the assembly of the consultation. The word 'empresa' is without the dollar sign $. If it’s a variable, it’s incorrect there. In this case, the query returns false.

I suggest the following code:

$query = "SELECT * FROM table WHERE empresa='$empresa' AND 
    MONTH(data_atendimento) = '$mes' AND YEAR(data_atendimento) = '$ano'";

if (!$sql = mysqli_query($conn, $query)){
   echo 'Sem registros...';
} else {
    $numRegistros = mysqli_num_rows($sql);
}
  • In this case company is not a variable

  • @Guilhermefreitas I get it... do you have access to the database environment to test using this Select right in it? Anyway, implement this suggestion I gave, because if you have no records for the query, the mysqli_query will return false

Browser other questions tagged

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