Query problem [duplicate]

0

You're giving the error Warning: mysqli_num_rows () expects parameter 1 to be mysqli_result, boolean given in. Can someone 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);
    
asked by anonymous 09.08.2018 / 20:14

1 answer

1

Guilherme, you may have some error in setting up the query. The word 'empresa' is missing the $ sign. If it is a variable, it is 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);
}
    
09.08.2018 / 20:29