What are some best practices for structuring SQL queries in PHP to optimize performance and reduce code complexity?

To optimize performance and reduce code complexity when structuring SQL queries in PHP, it is recommended to use prepared statements to prevent SQL injection attacks and improve query execution speed. Additionally, avoid dynamically building SQL queries with variables concatenated directly into the query string, as this can lead to security vulnerabilities and make the code harder to maintain.

// Example of using prepared statements to optimize performance and reduce code complexity
// Assume $pdo is a PDO object connected to the database

// Prepare the SQL query
$stmt = $pdo->prepare("SELECT * FROM users WHERE id = :id");

// Bind the parameter values
$stmt->bindParam(':id', $userId, PDO::PARAM_INT);

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

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

// Loop through the results
foreach ($results as $row) {
    // Process the data
}