Forum Moderators: coopster

Message Too Old, No Replies

Post mysql query with mail function

Try to send more than one records :-/ with no luck !

         

bethesda

9:45 pm on Nov 5, 2011 (gmt 0)

10+ Year Member



Hello everyone. First of all, sorry for my english.
I have a small problem with mail function and mysql_query.
I try to send mail with database record.
This is my code:


$query = "SELECT a, b, c, d FROM e WHERE x = '$something' ORDER BY date DESC";
$result = @mysql_query ($query);
$num = mysql_num_rows ($result);
if ($num > 0 ) {
echo '';
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$adres1 = "$email";
$adres2 = "$email2";
$all_adresses = "$adres1, $adres2";
$subject = "My Subject";
$body = "<table>";
$body .="<td>TEST: " . $row['1'] . "</td><tr>";
$body .="<td>TEST: " . $row['2'] . "</td><tr>";
$body .="<td>TEST: " . $row['3'] . "</td><tr>";
$body .="<td>TEST: " . $row['4'] . "</td><tr>";
$body .="<td>TEST: " . $row['5'] . "</td><tr>";
$body .="</table>";
}
mysql_free_result ($result);
} else {
$body = " " . mysql_error() . " ";
}


and


$extra_header_str = "MIME-Version: 1.0\r\n"."Content-type: text/html; charset=iso-8859-2\r\n"."From: my@email.com";
$mailsend = mail($all_adresses, $subject, $body, $extra_header_str);
echo "$mailsend";


well, im receives an email, unfortunately, it only gets a single record from the database. Should display 3 records.

Please assist . Thanks

Readie

4:15 pm on Nov 6, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
...
$body = "<table>";
...
}


You're overwriting the $body variable for every record you're retrieving. Try something a bit more like this:

$body = '';

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$body .= "<table>";
$body .="<td>TEST: " . $row['1'] . "</td><tr>";
$body .="<td>TEST: " . $row['2'] . "</td><tr>";
$body .="<td>TEST: " . $row['3'] . "</td><tr>";
$body .="<td>TEST: " . $row['4'] . "</td><tr>";
$body .="<td>TEST: " . $row['5'] . "</td><tr>";
$body .="</table>";
}

bethesda

4:51 pm on Nov 6, 2011 (gmt 0)

10+ Year Member



It works ! Thank you very much !