What are the advantages and disadvantages of putting each SQL query into a separate function in PHP?

By putting each SQL query into a separate function in PHP, you can improve code organization, reusability, and maintainability. Each function can encapsulate a specific query, making it easier to debug and modify in the future. However, this approach may lead to an increase in the number of function calls, which could affect performance in certain scenarios.

<?php

// Function to connect to the database
function connectToDB() {
    $servername = "localhost";
    $username = "username";
    $password = "password";
    $dbname = "myDB";

    $conn = new mysqli($servername, $username, $password, $dbname);

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

    return $conn;
}

// Function to execute a specific SQL query
function getUsers() {
    $conn = connectToDB();

    $sql = "SELECT * FROM users";
    $result = $conn->query($sql);

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

    $conn->close();
}

// Call the function to get users
getUsers();

?>