0

can you please help me to fix this issue? I want to return all the collected email address once. Here is the code. Comments are added.

function send_notification($ML_today) {

    // Getting expired contents from the database through a function
    $ML_send_to = check_expired($ML_today);

    if(count($ML_send_to) != 0){
        foreach ($ML_send_to as $ML_user_id) {
            $ML_query_email = mysql_query("SELECT `email` FROM `users` WHERE `userID` = '$ML_user_id'");
            $ML_row_3 = mysql_fetch_array($ML_query_email);
            $ML_email = $ML_row_3[0];

            $ML_to      = $ML_email;
            $ML_subject = 'Library notification';
            $ML_message = 'You have an expired library book to be returned. Please check your reservations.';
            $ML_headers = 'From: libray@example.com' . "\r\n" .
            'Reply-To: libraryr@example.com' . "\r\n" .
            'X-Mailer: PHP/' . phpversion();

            mail($ML_to, $ML_subject, $ML_message, $ML_headers);


            // Here I want to collect all the email addresss I sent emails and return once

            $ML_sent_all = // Stucked here

        }
        // Rerturning Emails
        return 'Message sent to: ' . $ML_sent_all;
    }
}
madhushankarox
  • 1,448
  • 1
  • 13
  • 17
  • I asked this because I don't know how to do this, marking negative and leaving is not a quality of a gentlemen. But thanks for the hitting negative :) – madhushankarox Sep 29 '12 at 00:46
  • You were probably voted down because you didn't answer the key questions that are needed in order to help with a problem: What have you tried so far? What does your code currently do? How is it different from what you want? – octern Sep 29 '12 at 00:53
  • Hi, I don't know much about this system. I'm new here. What are the key questions? Thank you! – madhushankarox Sep 29 '12 at 00:58
  • Your question actually was reasonably good. It had the relevant code included, and didn't include huge amounts of irrelevant detail. It also helps to say how you've tried to solve the problem so far. That makes it easier to find the exact problem you're having. It also shows that you made an effort, rather than just posting and expecting someone to solve it for you. – octern Sep 29 '12 at 01:08
  • Oh no. I was Googling and trying to fix this from hours now. Lastly I posted here. Actually I didn't focus key questions or anything bcz I was googling this and didn't fount stackoverflow answers there. Thank you. – madhushankarox Sep 29 '12 at 01:14

1 Answers1

2

First, you should use IN for better performance, like:

SELECT * FROM table WHERE id IN(2,3,5,7,11)

How to create this without falling in the lava pits of mysql_*? As this answer reports:

$ids     = $ML_send_to;
$inQuery = implode(',', array_fill(0, count($ids), '?'));

// you'll need to put your connection credentials here, see PDO docs
$db = new PDO(...);
$stmt = $db->prepare(
    'SELECT email
     FROM users
     WHERE userID IN(' . $inQuery . ')'
);

$stmt->execute($ids);

Loop over result, and build the sent-all string:

$ML_sent_all = "";

foreach($stmt -> fetchAll() as $row){

    $singleEmail = $row['email'];

    // do your stuff as before

    $ML_sent_all .= $singleEmail . ", ";

}

Trim the latest comma and space out;

$ML_sent_all = substr($ML_sent_all, 0 strlen($ML_sent_all) - 2);

PDO construct reference

Community
  • 1
  • 1
moonwave99
  • 21,957
  • 3
  • 43
  • 64