What are the potential pitfalls of sending emails in a SELECT statement function in PHP?

Sending emails within a SELECT statement function in PHP can lead to performance issues and potential errors since email sending operations are typically time-consuming and should be handled separately. To avoid this pitfall, it's recommended to retrieve the data first using the SELECT statement, then process the data and send emails outside of the SQL query.

// Retrieve data using SELECT statement
$query = "SELECT * FROM users WHERE status = 'active'";
$result = mysqli_query($connection, $query);

// Process data and send emails
while ($row = mysqli_fetch_assoc($result)) {
    $email = $row['email'];
    $message = "Hello, " . $row['name'] . "! This is a notification.";
    // Send email
    mail($email, 'Notification', $message);
}