Jump to content


Email addresses stored in a MySQL database

  • Please log in to reply
2 replies to this topic

#1 Teds455

  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 04 May 2006 - 04:15 AM


I'm looking for help on the above topic. I created a php script to go into a MySQL database, and send a message out to the stored email addresses of individuals meeting certain criteria. The problem I'm encountering is that as the first email is going out fine, then the second address in the database also gets the first one's info, the third both the second and first, and so on. I just want the code to generate 1 email at a time for all users, and I'm stuck on this... I've pasted the code below.


$connection = @mysql_connect("db.xxxxxx.com", "user_name", "password") or die("Couldn't connect.");

$db = mysql_select_db("user_name", $connection) or die("Couldn't select database.");

$sql = "SELECT * FROM test_email";

$sql_result = mysql_query($sql,$connection) or die ("Couldn't execute.");

while ($row = mysql_fetch_array($sql_result)) {

$id = $row["id"];
$email = $row["email"];
$name = $row["name"];
// email below
$msg .= "This is an automated email message to User:\t$name.\n\n";

$msg .= "Hello $name, how are you today?\n\n";

$mailheaders = "From: me@mine.com\n";

$mailheaders .= "Reply-To: me@mine.com";

mail($email, "Test Auto Email", $msg, $mailheaders);
echo "$id \t $email \t $name <p>";

Can anyone help?



#2 .josh

  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 04 May 2006 - 04:57 AM

i THINK your problem is one simple little dot

$msg [!--coloro:red--][span style=\"color:red\"][!--/coloro--].[!--colorc--][/span][!--/colorc--]= "This is an automated email message to User:\t$name.\n\n";

should be

$msg = "This is an automated email message to User:\t$name.\n\n";

(only on the first one, not all of them) cuz for each iteration of the while loop, you are continually concactinating the $msg var meaning it's just adding on the new stuff to the old stuff. if you take off the first " . " it re-inilitializes the variable.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 Teds455

  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 05 May 2006 - 10:29 AM

Crayon Violent-

You are an absolute genius!!

I know it was a simple problem, but how incredibly frustrating! I've been pushing back the expiration dates for these "broadcast emails" to be generated because I couldn't find info on this topic. After posting it on phpfreaks, you came up with the solution. Thanks!!!

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users