ruraldev Posted November 3, 2014 Share Posted November 3, 2014 Can anyone help me with why this code would only send 1 email when the query returns 8 rows? I am pretty sure it will be simple but I can't find the problem! $sql = "SELECT email, supplier_id FROM tbl_suppliers"; $stmt = $db->prepare($sql); $stmt->execute(); foreach($db->query($sql) as $row){ $supp_id = $row['supplier_id']; $emailCSV->setEmailMessage("some generic text message"); $email = $row['email']; $emailCSV->sendEmail("me@me.com",$email,"Quote Request"); } Quote Link to comment https://forums.phpfreaks.com/topic/292242-loop-not-working/ Share on other sites More sharing options...
Psycho Posted November 3, 2014 Share Posted November 3, 2014 Your code makes no sense. You prepare the query into the object $stmt, but then run the query on the unprepared string $sql. There is no need to prepare it since there are not variables in the query. So, lines 2 and 3 are unnecessary. As to your original question, how do you know there are 8 records in the result set? Have you verified the email address to be used? Echo some text to the page for debugging: $sql = "SELECT email, supplier_id FROM tbl_suppliers"; foreach($db->query($sql) as $row) { $email = $row['email']; $supp_id = $row['supplier_id']; echo "Email: {$email}, Supplier. ID: {$supp_id}<br>\n"; $emailCSV->setEmailMessage("some generic text message"); $emailCSV->sendEmail("me@me.com", $email, "Quote Request"); } Quote Link to comment https://forums.phpfreaks.com/topic/292242-loop-not-working/#findComment-1495612 Share on other sites More sharing options...
ruraldev Posted November 3, 2014 Author Share Posted November 3, 2014 Thanks for the help, I suppose that's what happens when I take bits of code from various samples! I have run the query manually to make sure it returns the 8 rows of data and it does work. I'll try outputting text instead of sending emails to see what happens. Cheers Gordon Quote Link to comment https://forums.phpfreaks.com/topic/292242-loop-not-working/#findComment-1495630 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.