Forum Moderators: coopster

Message Too Old, No Replies

Storing e-mail address from mysql query to a variable

         

Cactus_Jack

7:29 pm on Jan 22, 2010 (gmt 0)

10+ Year Member



Hello all;

I am trying to query a field in the DB and if the condition is met the email address is stored in a variable. I foresee having 30 or 40 email addresses stored in the variable The next phase would be to use the variable in the mail () function.

The end goal is to let someone update a profile page and indicate if they want to receive an email, then when I run the mail program is dynamically builds the email list.

Here's my code:

<?php require_once('Connections/connSiteDB.php'); ?>

<?php

$to = "SELECT `email` FROM `temp_registration` WHERE `stand_email` = 'y'";

$subject ="Weekly newsletter";

$message = "The message text goes here";

mail($to, $subject, $message)

?>

Matthew1980

7:41 pm on Jan 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Cactus_Jack,

You will need to do a loop through the returned array and use the mail function inside the loop for each result returned:-

$mail_r = "SELECT `email` FROM `temp_registration` WHERE `stand_email` = 'y'";

$mail_q = mysql_query($mail_r,$conn_data) or die(mysql_error());
$mail_f = mysql_fetch_object($mail_q,$conn_data) or die(mysql_error());

foreach( $mail_f->email AS $to)
{
$subject ="Weekly newsletter";

$message = "The message text goes here";

mail($to, $subject, $message)
}

The idea is that for each value returned the foreach loop sends the email to the address pulled from the db with the $message & $subject variable included.

There are other functions that return arrays but my own preference is Mysql_fetch_object()

I hope that makes sense, thats the idea anyway,

Hope this helps a little,

MRb

Cactus_Jack

9:28 pm on Jan 22, 2010 (gmt 0)

10+ Year Member



Thank you for your reply, I'm a novice and might be missing something. I now get this error when I run the code:

Parse error: parse error, unexpected '}' in /home/content/p/6/0/p60012/html/test.php on line 20

Here is the code after I added your syntax:

<?php require_once('Connections/connSiteDB.php'); ?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<?php

$mail_r = "SELECT `email` FROM `temp_registration` WHERE `stand_email` = 'y'";

$mail_q = mysql_query($mail_r,$conn_data) or die(mysql_error());

$mail_f = mysql_fetch_object($mail_q,$conn_data) or die(mysql_error());

foreach( $mail_f -> email AS $to)
{
$subject ="Weekly newsletter";
$message = "The message text goes here";
mail($to, $subject, $message)
}

?>

</body>
</html>

dreamcatcher

10:21 pm on Jan 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You are missing a semi colon after:

mail($to, $subject, $message)

should be:

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

Also, you never use foreach loops to iterate through a database array. Always use a while loop. You might also want to include a header as well with your site name/email.


$subject ="Weekly newsletter";
$message = "The message text goes here";
$query = mysql_query("SELECT `email` FROM `temp_registration` WHERE `stand_email` = 'y'") or die(mysql_error());

while ($mail_r = mysql_fetch_object($query)) {
mail($mail_r->email, $subject, $message,'From:"Your Website"<you@youremail.com>');
}

dc