Is it recommended to create a separate function for querying a single value from a database in PHP?

It is recommended to create a separate function for querying a single value from a database in PHP to promote code reusability, readability, and maintainability. By encapsulating the database query logic in a separate function, it becomes easier to make changes or updates to the query without affecting other parts of the code.

function getSingleValueFromDatabase($connection, $query) {
    $result = mysqli_query($connection, $query);
    
    if ($result) {
        $row = mysqli_fetch_array($result);
        return $row[0];
    } else {
        return false;
    }
}

// Example of how to use the function
$connection = mysqli_connect("localhost", "username", "password", "database");
$query = "SELECT COUNT(*) FROM users";
$count = getSingleValueFromDatabase($connection, $query);
echo "Total number of users: " . $count;