What are the potential pitfalls of using sprintf() function in PHP for constructing SQL queries, as seen in the provided code snippet?

Using sprintf() function in PHP for constructing SQL queries can potentially lead to SQL injection vulnerabilities if not properly sanitized. To avoid this issue, it is recommended to use prepared statements with parameterized queries instead. This helps prevent malicious users from injecting SQL code into the query.

// Fix using prepared statements with parameterized queries

// Establish a database connection
$pdo = new PDO("mysql:host=localhost;dbname=mydatabase", "username", "password");

// Prepare a SQL statement with a parameter
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username");

// Bind the parameter value
$stmt->bindParam(':username', $username);

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

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

// Process the results as needed
foreach ($results as $row) {
    // Handle each row
}