elmas156 Posted November 1, 2010 Share Posted November 1, 2010 I'm trying to take a number of items from a database using a while loop and put the results into a variable so that I can send one email with all the results. I'm having trouble figuring this out though. Can anyone please give me any ideas as to how to make this work? Here's what I've done so far... all it does is result in an email with the first set of results in the while loop, not all of them. <?php include("conf.inc.php"); $result = mysql_query("SELECT `to`,`from`,`subject`,`message`,`date` FROM allmsgs WHERE reported = 'n' ORDER BY `messid` ASC"); $row = mysql_fetch_row($result); $cdate = date('m-d-Y'); $sendto = "List@emails.here"; $emailsubject = "Webstats Report For $cdate."; while ($row = mysql_fetch_row($result)) { $to = $row[0]; $from = $row[1]; $subject = $row[2]; $message = $row[3]; $datetime = $row[4]; $eachmessage = "<p> <table width=\"400\"> <tr> <td> <hr width=\"400\"> To: $to<br> From: $from<br> On $datetime<br> <br> $subject<br> <br> $message </td> </tr> </table> </p>"; } $emailmessage = "<html> <body> <p>Here is a list of the messages that have been exchanged in the last 24 hours using the webstat system system.</p> $eachmessage </body> </html>"; // To send HTML mail, the Content-type header must be set $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; // Additional headers $headers .= 'From: Webstats <reports@webstats.com>' . "\r\n"; // Mail it mail($sendto, $emailsubject, $emailmessage, $headers); ?> Quote Link to comment Share on other sites More sharing options...
Andy-H Posted November 1, 2010 Share Posted November 1, 2010 <?php include("conf.inc.php"); $result = mysql_query("SELECT `to`,`from`,`subject`,`message`,`date` FROM allmsgs WHERE reported = 'n' ORDER BY `messid` ASC"); $cdate = date('m-d-Y'); $sendto = "List@emails.here"; $emailsubject = "Webstats Report For $cdate."; $eachmessage = ''; while ($row = mysql_fetch_row($result)) { $to = $row[0]; $from = $row[1]; $subject = $row[2]; $message = $row[3]; $datetime = $row[4]; $eachmessage .= "<p> <table width=\"400\"> <tr> <td> <hr width=\"400\"> To: $to<br> From: $from<br> On $datetime<br> <br> $subject<br> <br> $message </td> </tr> </table> </p>"; } $emailmessage = "<html> <body> <p>Here is a list of the messages that have been exchanged in the last 24 hours using the webstat system system.</p> $eachmessage </body> </html>"; // To send HTML mail, the Content-type header must be set $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; // Additional headers $headers .= 'From: Webstats <reports@webstats.com>' . "\r\n"; // Mail it mail($sendto, $emailsubject, $emailmessage, $headers); ?> You need to concatenate each message to the end of the string using the .= operator like above. Also the variable needs to be pre-defined outside (before) the loop in order to avoid a warning error. Quote Link to comment Share on other sites More sharing options...
elmas156 Posted November 1, 2010 Author Share Posted November 1, 2010 OK, thanks for the help Andy. However, this displays all the entries except for the first one found with the while loop. Any idea why it's excluding the first entry? Quote Link to comment Share on other sites More sharing options...
Andy-H Posted November 1, 2010 Share Posted November 1, 2010 No idea, did you add the $row = mysql_fetch_row($result); line back after your query. Not sure if it's relevant but it is if mysql_fetch_row doesn't reset the internal pointer on the resource. Quote Link to comment Share on other sites More sharing options...
elmas156 Posted November 1, 2010 Author Share Posted November 1, 2010 That was it... I had added the $row... line after the query. Thanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.