Jump to content

Recommended Posts

Hi,

I have got an array of users using a PHP program. However I am now trying to SELECT email FROM Member WHERE user='$user'

 

Once I have somehow selected all the email addresses I want to be able to send the same email to each of them!

 

The code I have attempted to make so far is....

<?php

include("../dbinfo.inc.php");

$cxn = mysqli_connect($host,$username,$password,$database) 
or die ("Couldn't connect to server."); 

$game="Chelsea V Liverpool";   ////// MANUALLY CHANGE THE VARIABLE
$query  = "SELECT DISTINCT user FROM history WHERE game='$game'"; ///////// SELECTS RELEVANT USERS
$result = mysqli_query($cxn,$query)
            or die ("Couldn't execute query.");

while($row = mysqli_fetch_assoc($result)) /// Take all the results

$tsubject = "subject";
$ttext = "text";
$sender = "sender@sender.com";
@mail({$row['user']}, $tsubject, $ttext, "FROM: $sender"); /////////// SENDS EMAIL
}
?>

The problem I have is somehow putting all the users email addresses in to the line.

	@mail({$row['user']}, $tsubject, $ttext, "FROM: $sender"); 

But I dont have a clue how to! Any suggesstions are much appreciated! thanks

This will put all the emails in a single string, comma separated.

 

<?php

$recipients = new array();
while($row = mysqli_fetch_assoc($result)) {
    $recipients[] = $row['user'];
}
$recipient_list = implode(", ", $recipients);

?>

The code I provided takes the email addresses from the query and puts them into a single string that is comma separated. You just need to use that value ($recipient_list) as the first parameter in the email() function:

 

<?php

include("../dbinfo.inc.php");

$cxn = mysqli_connect($host,$username,$password,$database) 
or die ("Couldn't connect to server."); 

$game="Chelsea V Liverpool";   ////// MANUALLY CHANGE THE VARIABLE
$query  = "SELECT DISTINCT user FROM history WHERE game='$game'"; ///////// SELECTS RELEVANT USERS
$result = mysqli_query($cxn,$query) or die ("Couldn't execute query.");

$recipients = new array();
while($row = mysqli_fetch_assoc($result)) {
    $recipients[] = $row['user'];
}
$recipient_list = implode(", ", $recipients);

$tsubject = "subject";
$ttext = "text";
$sender = "sender@sender.com";

@mail($recipient_list, $tsubject, $ttext, "FROM: $sender"); /////////// SENDS EMAIL

?>

Tried that but it came up with an error "Parse error: syntax error, unexpected T_ARRAY, expecting T_STRING or T_VARIABLE or '$' in .....l.php on line 12"

 

The actual email addresses have to be taken from another table in the database called Member. The two tables history and Member have the columns 'user' in common. This may be something to do with it.

Also could you tell me what this part does

$recipients = new array();

 

thanks

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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