Is it advisable to use a separate function to handle SQL queries in a TPL system?

It is advisable to use a separate function to handle SQL queries in a TPL system as it helps to keep the code organized, maintainable, and reusable. By separating the SQL query logic into its own function, it also makes it easier to make changes or updates to the queries without affecting other parts of the code.

// Function to handle SQL queries
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 SQL query
    $result = $conn->query($sql);

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

    return $result;
}

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

// Process the query result
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "Name: " . $row["name"] . "<br>";
    }
} else {
    echo "0 results";
}