Jump to content

Sending results of an SQL query via email


CalvinSmith

Recommended Posts

Please help -I have now searched for days for an answer to this!

I am attempting to query a database and send the results in table format via email (so I assume in HTML). I can query the table correctly and display the information but how do I now send it - if at all possible???

So far:

 

<?php

$con = mysql_connect("*****","*****","*****");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }

  mysql_select_db("*****", $con);

  $result = mysql_query("SELECT * FROM contactdetails");

  echo "<table border='1'>

  <tr>

    <th>Name:</th>

    <th>Surname:</th>

    <th>Number:</th>

    <th>Email:</th>

    <th>Moving From:</th>

    <th>Moving To:</th>

    <th>Date:</th>

  </tr>";

 

  while($row = mysql_fetch_array($result)){

    echo "<tr>";

    echo "<td>" . $row['Name'] . "</td>";

    echo "<td>" . $row['Surname'] . "</td>";

    echo "<td>" . $row['Number'] . "</td>";

    echo "<td>" . $row['Email'] . "</td>";

    echo "<td>" . $row['MovingFrom'] . "</td>";

    echo "<td>" . $row['MovingTo'] . "</td>";

    echo "<td>" . $row['Date'] . "</td>";

    echo "</tr>";

  }

  echo "</table>";

 

  mysql_close($con);

?>

 

I tried PHP's mail function but I obviously did it all wrong:

 

<?php

$con = mysql_connect("*****","*****","*****");

if (!$con)

{

die('Could not connect: ' . mysql_error());

}

mysql_select_db("*****", $con);

$result = mysql_query("SELECT * FROM contactdetails");

$to = "[email protected]";

$subject = "Results from query";

$body = "

echo "<table border='1'>

<tr>

<th>Name:</th>

<th>Surname:</th>

<th>Number:</th>

<th>Email:</th>

<th>Moving From:</th>

<th>Moving To:</th>

<th>Date:</th>

</tr>";

 

while($row = mysql_fetch_array($result)){

echo "<tr>";

echo "<td>" . $row['Name'] . "</td>";

echo "<td>" . $row['Surname'] . "</td>";

echo "<td>" . $row['Number'] . "</td>";

echo "<td>" . $row['Email'] . "</td>";

echo "<td>" . $row['MovingFrom'] . "</td>";

echo "<td>" . $row['MovingTo'] . "</td>";

echo "<td>" . $row['Date'] . "</td>";

echo "</tr>";

}

echo "</table>";

mysql_close($con);

";

$headers = "From: [email protected]";

mail($to,$subject,$body,$headers);

echo "Mail sent to $to";

?>

 

Please help

My thanks in anticipation

 

Thanks in anticipation!

You're doing almost correct :)  just fix your code as indicated below:

 

$body = "<table border='1'>
<tr>
<th>Name:</th>
<th>Surname:</th>
<th>Number:</th>
<th>Email:</th>
<th>Moving From:</th>
<th>Moving To:</th>
<th>Date:</th>
</tr>";

while($row = mysql_fetch_array($result)){
$body.="<tr>";
$body.="<td>" . $row['Name'] . "</td>";
$body.="<td>" . $row['Surname'] . "</td>";
$body.="<td>" . $row['Number'] . "</td>";
$body.="<td>" . $row['Email'] . "</td>";
$body.="<td>" . $row['MovingFrom'] . "</td>";
$body.="<td>" . $row['MovingTo'] . "</td>";
$body.="<td>" . $row['Date'] . "</td>";
$body.="</tr>";
}
$body.="</table>";

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.