How can one ensure compatibility with both procedural and object-oriented approaches when using mysqli_query() in PHP?

When using mysqli_query() in PHP, one can ensure compatibility with both procedural and object-oriented approaches by checking the connection type before executing the query. If the connection is procedural, use mysqli_query() directly. If the connection is object-oriented, use the query() method of the mysqli object to execute the query.

// Check connection type and execute query accordingly
if ($connection instanceof mysqli) {
    // Object-oriented approach
    $result = $connection->query($sql);
} else {
    // Procedural approach
    $result = mysqli_query($connection, $sql);
}