What are some best practices for bundling SQL queries through functions or abstraction layers in PHP?

When working with SQL queries in PHP, it is best practice to bundle them through functions or abstraction layers to improve code reusability, readability, and security. By abstracting SQL queries into functions, you can easily reuse them throughout your codebase, make changes in a single location, and prevent SQL injection attacks.

// Example of bundling SQL queries through functions in PHP

// Function to fetch user data from the database
function getUserData($userId, $conn) {
    $sql = "SELECT * FROM users WHERE id = :id";
    $stmt = $conn->prepare($sql);
    $stmt->bindParam(':id', $userId);
    $stmt->execute();
    return $stmt->fetch();
}

// Example of using the above function
$userId = 1;
$userData = getUserData($userId, $conn);
print_r($userData);