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.

Link to comment
Share on other sites

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("me@example.com", "Subject", $message);

 

Edit: D'oh

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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. :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.