CalvinSmith Posted April 20, 2010 Share Posted April 20, 2010 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! Link to comment https://forums.phpfreaks.com/topic/199111-sending-results-of-an-sql-query-via-email/ Share on other sites More sharing options...
Sergey Popov Posted April 20, 2010 Share Posted April 20, 2010 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>"; Link to comment https://forums.phpfreaks.com/topic/199111-sending-results-of-an-sql-query-via-email/#findComment-1045061 Share on other sites More sharing options...
CalvinSmith Posted April 20, 2010 Author Share Posted April 20, 2010 Thank you! That sends the results to the email which is fantastic. But the email seems to loose its formatting and doesnt come out in table form ... Any suggestions? Link to comment https://forums.phpfreaks.com/topic/199111-sending-results-of-an-sql-query-via-email/#findComment-1045072 Share on other sites More sharing options...
CalvinSmith Posted April 20, 2010 Author Share Posted April 20, 2010 Figured it out - thank you! Ok last one - how do I make this form only retrieve NEW data from the database? Link to comment https://forums.phpfreaks.com/topic/199111-sending-results-of-an-sql-query-via-email/#findComment-1045074 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.