简体   繁体   中英

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 = "example@example.com";
$from = "example@example.com";
$subject = "Test";
$message = "$Items";
$headers = "MIME-Version: 1.0 \r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1 \r\n";
$headers .= "From: example@example.com";
mail($to, $subject, $message, $headers);
}

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 = "example@example.com";
      $from = "example@example.com";
      $subject = "Test";
      $message = "$Items";
      $headers = "MIME-Version: 1.0 \r\n";
      $headers .= "Content-type: text/html; charset=iso-8859-1 \r\n";
      $headers .= "From: example@example.com";
      mail($to, $subject, $message, $headers);
 }

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: example@example.com";
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.

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 = "example@example.com";
$subject = "Test";
$message = "$Items";
$headers = "MIME-Version: 1.0 \r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1 \r\n";
$headers .= "From: example@example.com";
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM