What best practices should be followed when formatting MySQL queries in PHP to improve readability and maintainability?

When formatting MySQL queries in PHP, it is important to follow best practices to improve readability and maintainability. One common approach is to separate the query string from the PHP code to make it easier to read and modify. Another best practice is to use prepared statements to prevent SQL injection attacks and improve performance. Additionally, using proper indentation and spacing can make the code more organized and easier to understand.

// Example of formatting MySQL query in PHP using best practices

// Separate the query string from the PHP code
$query = "SELECT * FROM users WHERE id = ?";
$stmt = $pdo->prepare($query);

// Use prepared statements to prevent SQL injection
$stmt->bindParam(1, $id, PDO::PARAM_INT);
$stmt->execute();

// Loop through the results
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    // Process the data
}