Jump to content

loop not working


ruraldev

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("[email protected]",$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("[email protected]", $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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.