What are the potential pitfalls when trying to outsource a SELECT query in PHP and how can they be avoided?

When trying to outsource a SELECT query in PHP, one potential pitfall is the risk of SQL injection if user input is not properly sanitized. To avoid this, always use prepared statements with parameterized queries to prevent malicious SQL injection attacks.

// Connect to database
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');

// Prepare the statement
$stmt = $pdo->prepare('SELECT * FROM users WHERE id = :id');

// Bind parameters
$stmt->bindParam(':id', $id, PDO::PARAM_INT);

// Execute the query
$stmt->execute();

// Fetch the results
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Output the results
print_r($results);