How can errors like "mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given" be avoided when comparing dates in PHP?

When comparing dates in PHP, it is important to ensure that the query executed successfully and returned a valid result set before trying to fetch the number of rows. This error occurs when the query fails and returns a boolean value (false) instead of a mysqli_result object. To avoid this error, you should check if the query was successful before using mysqli_num_rows() function.

$result = mysqli_query($connection, "SELECT * FROM table WHERE date = '2022-01-01'");
if($result) {
    $num_rows = mysqli_num_rows($result);
    echo "Number of rows: " . $num_rows;
} else {
    echo "Error executing query: " . mysqli_error($connection);
}