How can SQL database queries be optimized for retrieving domain names in PHP?

When retrieving domain names from an SQL database in PHP, it is important to optimize the query to ensure efficient performance. One way to do this is by selecting only the necessary columns and using appropriate indexing on the domain name column. Additionally, using prepared statements can help prevent SQL injection attacks and improve security.

// Assuming $pdo is the PDO object connected to the database

// Optimize the query by selecting only the necessary columns and using indexing
$stmt = $pdo->prepare("SELECT domain_name FROM domains_table WHERE condition = :condition");
$stmt->bindParam(':condition', $condition);
$stmt->execute();

// Fetch domain names from the result set
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $domainName = $row['domain_name'];
    // Process the domain name as needed
}