How can the SQL query be optimized to directly check for the domain extension in the database?
The SQL query can be optimized by using the SUBSTRING_INDEX function to directly check for the domain extension in the database. This function allows us to extract the domain extension from the email address and compare it directly in the WHERE clause of the query. This can improve performance by avoiding unnecessary string manipulations or data retrieval.
$email = 'example@email.com';
$domain_extension = substr(strrchr($email, '.'), 1);
$sql = "SELECT * FROM users WHERE SUBSTRING_INDEX(email, '.', -1) = :domain_extension";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':domain_extension', $domain_extension);
$stmt->execute();
$results = $stmt->fetchAll();