What are the advantages of encapsulating SQL queries in PHP functions for database management and maintenance?

Encapsulating SQL queries in PHP functions for database management and maintenance helps improve code organization, readability, and reusability. It also enhances security by preventing SQL injection attacks and makes it easier to make changes to queries in one central location.

function executeQuery($sql) {
    // Connect to the database
    $conn = new mysqli("localhost", "username", "password", "database");

    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    // Execute the query
    $result = $conn->query($sql);

    // Close the connection
    $conn->close();

    return $result;
}

// Example usage
$sql = "SELECT * FROM users";
$result = executeQuery($sql);