How can the use of prepared statements and parameterized queries enhance the security and performance of database interactions in PHP?

Using prepared statements and parameterized queries in PHP can enhance security by preventing SQL injection attacks, as they separate SQL logic from user input. This also improves performance by allowing the database to optimize query execution plans.

// Using prepared statements and parameterized queries in PHP
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username");
$stmt->bindParam(':username', $username);
$stmt->execute();
$results = $stmt->fetchAll();