What best practices should PHP developers follow when working with SQL queries to ensure optimal performance and security?

When working with SQL queries in PHP, developers should use prepared statements to prevent SQL injection attacks and improve performance by reusing query execution plans. Additionally, developers should avoid dynamically constructing SQL queries with user input to reduce the risk of SQL injection vulnerabilities.

// Using prepared statements to prevent SQL injection
$stmt = $pdo->prepare('SELECT * FROM users WHERE username = :username');
$stmt->execute(['username' => $username]);
$results = $stmt->fetchAll();