-3

I have a column (named user_email) in a table called (event) in my database, This column has email addresses. I want to get all these email addresses and send one email for all of them. I tried to use 'for'loop but it only sends the email to one email only, I might be using for in a wrong way, any suggestion,

Part of my code

$select="SELECT * FROM event WHERE event_title='$title'";
$query_select= mysql_query($select);
$row = mysql_fetch_array($result); 

$email_to=$row["user_email"];

 for($x=0;$x<count($row['user_email']);$x++){
        @mail($email_to,$email_subject,$email_message,$header);



    }
John Conde
  • 217,595
  • 99
  • 455
  • 496
SamQ
  • 1
  • 1
  • If you only want to send one email append the emails to one variable, I think comma separated, and move the mail function outside the for loop. Also when debugging never use the `@` that will hide any useful error messages. – chris85 May 08 '15 at 02:02
  • Use `while ($row = mysql_fetch_array($query_select))` instead of your for loop. Also, you should really be using PDO. – Stuart Wagner May 08 '15 at 02:03

2 Answers2

3

You made it waaaayyy more complicated than it needs to be. Just loop through your results and send your email:

$select="SELECT * FROM event WHERE event_title='$title'";
$result = mysql_query($select);
while ($row = mysql_fetch_array($result)) {
     mail($row['user_email'],$email_subject,$email_message,$header);
}

You really need to improve upon this as you don't check for mail errors (and even hide any PHP is trying to tell you about).

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Community
  • 1
  • 1
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • Conode, I tried that before I try the for loop, but it didn't do the job – SamQ May 08 '15 at 02:07
  • How did it not work? That code is working code you need to figure out where you made an error. Are you sure you're getting results that match your query? Does MySQL report an error? – John Conde May 08 '15 at 02:08
  • Use `$query_select` in the `while` declaration, as that is what you set the results to on the line above. Edit: That works too ;) – Stuart Wagner May 08 '15 at 02:11
  • @John Conde when i used it , it only sent the email for one address and ignored the rest – SamQ May 08 '15 at 02:12
  • You didn't have your loop right. Use this *exact* code and it *will* work. – John Conde May 08 '15 at 02:13
  • @JohnConde no errors are shown, the address was sent the email is their and fetched correctly, but i also need that to be done for other addresses – SamQ May 08 '15 at 02:13
0

You need a while loop. This way it iterates over all the rows in your result set. I had to guess at your column names so you'll need to fix this most likely

while($row = mysql_fetch_assoc($query_select)) {
    @mail($row['email_to'],$row['email_subject'],$row['email_message'],$header);
}
Machavity
  • 30,841
  • 27
  • 92
  • 100
  • The think the mail parameters, other than to recipient, is defined elsewhere and static. – John Conde May 08 '15 at 02:05
  • Yeah. I just left his placeholders. It's all pseudo-code anyways without more of the code to work with – Machavity May 08 '15 at 02:06
  • You should have `$query_select` in the `while` declaration, as that is what OP is setting his results to. – Stuart Wagner May 08 '15 at 02:12
  • 1
    @StuartWagner Good catch. I copied and pasted what he had – Machavity May 08 '15 at 02:35
  • @StuartWagner I just realized that my original code was fine and i didn't need to change it. Before, I had both, gmail and hotmail addresses in my sql table, and the email wasn't being sent to all until i changed all the emails addresses in the table to gmail. (for some reason i don't know) – SamQ May 08 '15 at 02:44