How can prepared statements be utilized in PHP to improve security and performance when interacting with a MySQL database?

Prepared statements can be utilized in PHP to improve security and performance when interacting with a MySQL database by separating SQL logic from data input, preventing SQL injection attacks, and reducing the need for repetitive query parsing.

// Create a prepared statement
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username");

// Bind parameters
$stmt->bindParam(':username', $username, PDO::PARAM_STR);

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

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