Click to See Complete Forum and Search --> : querying tables and send through mail


tdsmithj
03-05-2008, 08:12 AM
I want to query a database then send the database list to me through e-mail. I have managed to get the script to send me records through e-mail but it only shows the last record of the table.

i tried implementing a while loop but unsure how to implement it


<?php include('dbconfig.php');

$to = "<my e-mail>";
$subject = "Todays registrations " . date("d - m - Y");
$result = mysql_query ("select * FROM members where DateReg = Curdate()") or die (mysql_error());
while($row = mysql_fetch_array($result)) {
$message =
"Todays registers
ID: $row[ID]
First Name: $row[FirstName]
Surname: $row[SurName]
Badge: $row[BadgeName]
E-mail: $row[Email]
DOB: $row[Dob]
Address: $row[Address]
Mobile: $row[Mobile]
Secret Question: $row[SecQuest]
Secret Answer: $row[SecAnwer]
Emergency Name: $row[EmergencyName]
Emergency Number: $row[EmergencyNum]
Additional Notes: $row[Disabilities]
Date Registered: $row[DateReg]";
}

$from = "Registrations";
$headers = "From: $from";
mail($to,$subject,$message,$headers);
?>


any help how i can display all records?

Sorehead
03-05-2008, 08:28 AM
What your doing wrong is you are overwriting the message variable each time you loop through the records, you need to add each record on. so..


<?php include('dbconfig.php');

$to = "<my e-mail>";
$subject = "Todays registrations " . date("d - m - Y");
$result = mysql_query ("select * FROM members where DateReg = Curdate()") or die (mysql_error());
$message="";
while($row = mysql_fetch_array($result)) {
$message .=
"Todays registers
ID: $row[ID]
First Name: $row[FirstName]
Surname: $row[SurName]
Badge: $row[BadgeName]
E-mail: $row[email]
DOB: $row[Dob]
Address: $row[Address]
Mobile: $row[Mobile]
Secret Question: $row[SecQuest]
Secret Answer: $row[SecAnwer]
Emergency Name: $row[EmergencyName]
Emergency Number: $row[EmergencyNum]
Additional Notes: $row[Disabilities]
Date Registered: $row[DateReg] \n\n";
}

$from = "Registrations";
$headers = "From: $from";
mail($to,$subject,$message,$headers);
?>


All ive did is change your
message =
to
message .=
which adds each record to the string.
I also added a couple of line breaks so you can see where one record ends and another begins.