Forum Moderators: coopster

Message Too Old, No Replies

a PHP database loop output question

         

field4000

3:46 am on Nov 10, 2006 (gmt 0)

10+ Year Member



Hey all,

I am trying to loop through a series of email addresses from an existing database. The email address will be used in the following code to send an HTML email:

<?php

include '../includes/php/config.php';

$query = "SELECT * FROM mailinglist ORDER BY id;";
$result = mysql_query($query);

$num = mysql_numrows($result);

mysql_close();

$i = 0;
while ($i < $num) {

$emailaddress = mysql_result($result, $i, "emailaddress");

$number = $i + 1;

$to = '$emailaddress';

$subject = 'Subject Here';

$message = '
<html>

...code here...

</html>';

$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

$headers .= 'From: Hideaway Beds <email@address.com.au>' . "\r\n";

mail($to, $subject, $message, $headers);

$i++;
}

?>

I am having trouble getting the script to loop through and insert the email addresses into the script from the database.

Any help on this would be much appreciated.

Cheers.

eelixduppy

6:36 am on Nov 10, 2006 (gmt 0)



Your code has some flaws in it. Fixed:
include '../includes/php/config.php';

$query = "SELECT * FROM mailinglist ORDER BY id;";
$result = mysql_query($query);
while ($row = mysql_fetch_array [us2.php.net]($result)) {

$to = $row["emailaddress"];
$subject = 'Subject Here';
$message = '
<html>

...code here...

</html>';
$headers = 'MIME-Version: 1.0'."\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1'."\r\n";
$headers .= 'From: Hideaway Beds <email@address.com.au>'."\r\n";
mail($to, $subject, $message, $headers);
}

This will work, however it isn't recommended that you use this method for mass mail. For this, you should use PEAR::Mail [pear.php.net].

Good luck!