How can one improve the readability and maintainability of SQL queries in PHP projects?

To improve the readability and maintainability of SQL queries in PHP projects, one can use prepared statements instead of directly embedding variables into the query string. Prepared statements not only make the code more secure by preventing SQL injection attacks but also make the queries easier to read and maintain.

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

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

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

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