Jump to content

[SOLVED] emailing formatted mysql table data


JohnHall

Recommended Posts

Hi - noob here,

 

I have a sql select query that returns rows from a table (5 columns - name, email addr, phone#, etc) and I have those results formatted and displayed using a while loop and an echo that puts the fields into a table.

 

while($row = mysqli_fetch_array($result)) {
$date = $row['timestamp'];
$office = $row['office'];
....

echo '<TR><TD style="width: 40pt">'.$date.'</TD><TD style="width: 40pt">'.$office.'</TD>......

 

 

That works fine, but now I would also like to e-mail the formatted table. I'm very new to php/mysql so don't know where to begin.

 

Any help is greatly appreciated.

instead of

echo '<TR><TD style="width: 40pt">'.$date.'</TD><TD style="width: 40pt">'.$office.'</TD>......

do

$body.= '<TR><TD style="width: 40pt">'.$date.'</TD><TD style="width: 40pt">'.$office.'</TD>......

the dot will concatenate each line onto the previous

then you just use $body in the body of your e-mail.

Instead of echo'ing to the screen, concatinate to a variable. You can then just use that variable as the body text for an e-mail.

 

$message = "";
while($row = mysqli_fetch_array($result)) {
   $date = $row['timestamp'];
   $office = $row['office'];

   ...

   $message .= '<TR><TD style="width: 40pt">'.$date.'</TD><TD style="width: 40pt">'.$office.'</TD>......';
} 

mail("[email protected]", "Subject", $message);

 

Edit: D'oh

Thanks - works great!

 

I had actually tried that before - but I got the undefined variable error, as I did this time as well. So I set the $message = NULL; at the beginning of the script, and it works now. Is doing that proper though - in terms of good coding practice?

 

Thanks again!

Undefined variable just means your using a variable that doesn't currently exist. In this case it doesn't really matter as it just creates it for you. It was to prevent the notice that I put message at the top equal to an empty string. :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.