Are there any best practices for optimizing PHP scripts that interact with a MySQL database to prevent performance issues?

To optimize PHP scripts that interact with a MySQL database and prevent performance issues, it is important to use prepared statements to prevent SQL injection attacks, minimize the number of queries executed, avoid using SELECT * to only fetch necessary columns, index frequently queried columns, and utilize caching mechanisms to reduce database load.

// Example of using prepared statements to prevent SQL injection
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username");
$stmt->bindParam(':username', $username, PDO::PARAM_STR);
$stmt->execute();
$result = $stmt->fetchAll();