Warning: mysqli_num_rows () expects parameter 1 to be mysqli_result, boolean given in [duplicate]

1
    $sql = "SELECT 'id', 'nome', 'nivel' FROM 'usuarios' WHERE ('usuario' = '".$usuario ."') AND ('senha' = '". sha1($senha) ."') AND ('ativo' = 1) LIMIT 1";
    $result = mysqli_query($conn, $sql);

    if (mysqli_num_rows($result) != 1) {

        echo "Login inválido!"; exit;

    } else {
        //
    }

Error:

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in
    
asked by anonymous 25.09.2018 / 03:21

2 answers

0

The problem is that your query has an error, so mysqli_query returns false and passing this value to mysqli_num_rows causes error

Before the if prints on the screen the command ( echo $sql; ) and try to execute the same in phpMyAdmin, MySQL Workbench or something, these software will show more exactly where the error is

    
25.09.2018 / 03:28
0

To know how many rows have come, you can try to do this:

$result = mysqli_query($conn, $sql);
$arrDados = mysqli_fetch_assoc($result);
$nLinhas = count($arrDados);

if ($nLinhas == 1) {
    echo "Login Ok!";
    // Continua sua programação.
} else {
    echo "Login inválido!"; exit;
}       
    
25.09.2018 / 03:32