Jump to content

Recommended Posts

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");
}
Link to comment
https://forums.phpfreaks.com/topic/292242-loop-not-working/
Share on other sites

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");
}
Link to comment
https://forums.phpfreaks.com/topic/292242-loop-not-working/#findComment-1495612
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/292242-loop-not-working/#findComment-1495630
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.