How can developers improve the readability and maintainability of their PHP code when dealing with complex database queries?

Complex database queries in PHP can make code hard to read and maintain. To improve readability and maintainability, developers can use query builders or ORM libraries to abstract the database interactions and make the code more structured and easier to understand. Example:

// Using a query builder library to improve readability and maintainability

use QueryBuilder\QueryFactory;

$queryFactory = new QueryFactory($pdo);

$query = $queryFactory->newSelect();
$query->cols(['id', 'name'])
      ->from('users')
      ->where('status = :status')
      ->bindValue('status', 'active');

$statement = $pdo->prepare($query->getStatement());
$statement->execute($query->getBindValues());

$results = $statement->fetchAll(PDO::FETCH_ASSOC);

foreach ($results as $result) {
    echo $result['id'] . ' - ' . $result['name'] . '<br>';
}