...

View Full Version : Email Query Results



Kal
03-17-2008, 10:30 AM
Hi Guys,

I'm having trouble emailing the results of a query.

For example if I run the following query, I would get a list of status's and a frequency's.



SELECT DISTINCT status, COUNT(status) AS frequency FROM status GROUP BY status


For each row generated how can I email the results.

Any help would be great.

abduraooft
03-17-2008, 10:35 AM
www.php.net/mail

Kal
03-17-2008, 10:42 AM
Sorry I should have mentioned I know how to use the mail function, but I'm not sure how to loop through each row generated to be listed in an email when I send it.

I have the following, but when I run this it emails each row seperately, how can I send them all in one go?


<?php

require('database.php');

$query = "SELECT DISTINCT status, COUNT(status) AS frequency FROM status GROUP BY status";

$result = mysql_query ($query) or die ('Your query did not match any results: ' . mysql_error());

if (mysql_num_rows($result)>0)

{

while($row=mysql_fetch_array($result))

{

$to = 'xx@xxx.xx.xx';
$subject = 'Report';

$body = '<HTML>';

$body = $body . '<HEAD>';

$body = $body . '</HEAD>';

$body = $body . '<BODY>';

$body = $body . '<div width="500">';

$body = $body . '<table border="0" cellspacing="2" cellpadding="2">';

$body = $body . '<tr>';
$body = $body . '<td>';
$body = $body . $row['status'];
$body = $body . '</td>';
$body = $body . '<td>';
$body = $body . ' - ';
$body = $body . '</td>';
$body = $body . '<td>';
$body = $body . $row['frequency'];
$body = $body . '</td>';
$body = $body . '<tr>';

$body = $body . '</table>';

$body = $body . '</div>';

$body = $body . '</BODY>';

$body = $body . '</HTML>';

$headers = "MIME-Version: 1.0\r\n"."Content-type: text/html; charset=iso-8859-1\r\n"."From: noreply@xxx.xxx";

if($result)
{
mail($to, $subject, $body, $headers);
}
else
{
'';
}

}

}

?>

StupidRalph
03-17-2008, 01:06 PM
//Just wanted to let you see this real quick.
$body = "this is a string of text";
$body = $body . " this is another string";
echo $body;

//is the same as saying

$body2 = "this is a string of text";
$body2 .= " this is another string";
echo $body2;



You should take everything out of your loop except this part.



$body = $body . '<tr>';
$body = $body . '<td>';
$body = $body . $row['status'];
$body = $body . '</td>';
$body = $body . '<td>';
$body = $body . ' - ';
$body = $body . '</td>';
$body = $body . '<td>';
$body = $body . $row['frequency'];
$body = $body . '</td>';
$body = $body . '<tr>';


Just let the above part run build inside the loop then you'll finish building the HTML and email it later on in the script.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum