Bob Stone
Bob Stone

Reputation: 39

Php Email Mysql Fetch Array

I have this script to send an email containing information from my database. The user can have 1+ items in the database with it's location. So when I empty the rows that match the user the amount of emails sent equals the number of rows they have. So if they have 8 items in the database it send 8 emails. Each adds an item. So the first email has one item, the second with two items, and so on. I am trying to find a simple way to make it get all the information before sending the email so the customer only gets one email. The logical way would be to echo the information but I can't do that with a php variable. I didn't include the query and database connection in the code below. Any help would be loved.

while ($row = mysql_fetch_array($query)) {
$Items .=  $row['Items']. " - Aisle " .$row['Loc']. "<p>&nbsp;</p>";
$to = "[email protected]";
$from = "[email protected]";
$subject = "Test";
$message = "$Items";
$headers = "MIME-Version: 1.0 \r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1 \r\n";
$headers .= "From: [email protected]";
mail($to, $subject, $message, $headers);
}

Upvotes: 0

Views: 778

Answers (3)

Fabio
Fabio

Reputation: 23480

Your concatenation is wrong, you should first declare your variable as empty outside your loop

$Items = '';

Then start your loop and get all data you need concatenating your variable

while ($row = mysql_fetch_array($query)) 
{
    $Items .=  $row['Items']. " - Aisle " .$row['Loc']. "<p>&nbsp;</p>";
}

Now you are ready for send email, outside your loop or you will end up with one email for each cicle. So your code would look like this

$Items = '';
while ($row = mysql_fetch_array($query)) 
{
    $Items .=  $row['Items']. " - Aisle " .$row['Loc']. "<p>&nbsp;</p>";
}
$from = "[email protected]";
$subject = "Test";
$message = "$Items";
$headers = "MIME-Version: 1.0 \r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1 \r\n";
$headers .= "From: [email protected]";
mail($to, $subject, $message, $headers);

Then I would like you to remember that mysql_* functions are deprecated so i would advise you to switch to mysqli or PDO

Upvotes: 0

Eugene
Eugene

Reputation: 1959

Just move send function out of cycle:

while ($row = mysql_fetch_array($query)) {
     $Items .=  $row['Items']. " - Aisle " .$row['Loc']. "<p>&nbsp;</p>";

 }
 if ($Items != '') {
      $to = "[email protected]";
      $from = "[email protected]";
      $subject = "Test";
      $message = "$Items";
      $headers = "MIME-Version: 1.0 \r\n";
      $headers .= "Content-type: text/html; charset=iso-8859-1 \r\n";
      $headers .= "From: [email protected]";
      mail($to, $subject, $message, $headers);
 }

Upvotes: 1

Kevin
Kevin

Reputation: 513

When you iterate over the items, you should only build a message and not the entire email.. It's hard to do much more than the following without knowing more about your query. I'll give it a shot anyway:

$message = '';

while ($row = mysql_fetch_array($query)) {
     $Items =  $row['Items']. " - Aisle " .$row['Loc']. "<p>&nbsp;</p>";
     $message .= "$Items";
}

$headers = "MIME-Version: 1.0 \r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1 \r\n";
$headers .= "From: [email protected]";
mail($to, $subject, $message, $headers);

Note: I wouldn't implement this code as is. It's meant to serve as an example on how to structure your code.

Upvotes: 0

Related Questions