How can the error "query() on a non-object" be resolved in PHP when using MySQLi?

The error "query() on a non-object" typically occurs when the query execution fails and the mysqli query method returns false instead of a result object. To resolve this issue, you should check if the query execution was successful before trying to fetch results. This can be done by verifying if the query method returns a valid result object.

// Check if the query execution was successful before fetching results
$result = $mysqli->query($sql);
if($result) {
    // Process the query results
} else {
    echo "Error executing query: " . $mysqli->error;
}