472,331 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,331 software developers and data experts.

Sending results of an SQL query via email

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:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $con = mysql_connect("*****","*****","*****");
  3. if (!$con)
  4.   {
  5.   die('Could not connect: ' . mysql_error());
  6.   }
  7.   mysql_select_db("*****", $con);
  8.   $result = mysql_query("SELECT * FROM contactdetails");
  9.   echo "<table border='1'>
  10.   <tr>
  11.     <th>Name:</th>
  12.     <th>Surname:</th>
  13.     <th>Number:</th>
  14.     <th>Email:</th>
  15.     <th>Moving From:</th>
  16.     <th>Moving To:</th>
  17.     <th>Date:</th>
  18.   </tr>";
  19.  
  20.   while($row = mysql_fetch_array($result)){
  21.     echo "<tr>";
  22.     echo "<td>" . $row['Name'] . "</td>";
  23.     echo "<td>" . $row['Surname'] . "</td>";
  24.     echo "<td>" . $row['Number'] . "</td>";
  25.     echo "<td>" . $row['Email'] . "</td>";
  26.     echo "<td>" . $row['MovingFrom'] . "</td>";
  27.     echo "<td>" . $row['MovingTo'] . "</td>";
  28.     echo "<td>" . $row['Date'] . "</td>";
  29.     echo "</tr>";
  30.   }
  31.   echo "</table>";
  32.  
  33.   mysql_close($con);
  34. ?>
Thanks in anticipation!
Apr 20 '10 #1
9 14676
Atli
5,058 Expert 4TB
Hey.

There are several methods to send a mail in PHP.

The simplest (somewhat) is PHP's own mail function, which is usually used when there is an available sendmail or local SMTP server. It's best suited for plain-text mails and simple HTML mails.

You can also use one of the PHP mailer classes, like PHPMailer or SwiftMailer. They can be used to connect to most types of servers, local and remote. - For example, see this PHPMailer Gmail example.
Apr 20 '10 #2
Hi Alti,

Thank you for your response.
I've been trying to use PHP's mail function but I obviously have it all wrong.
This is what I've tried:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $con = mysql_connect("*****","*****","*****");
  3. if (!$con)
  4.   {
  5.   die('Could not connect: ' . mysql_error());
  6.   }
  7.   mysql_select_db("*****", $con);
  8.   $result = mysql_query("SELECT * FROM contactdetails");
  9.   $to = "me@example.com";
  10.   $subject = "Results from query";
  11.   $body = "
  12.   echo "<table border='1'>
  13.   <tr>
  14.     <th>Name:</th>
  15.     <th>Surname:</th>
  16.     <th>Number:</th>
  17.     <th>Email:</th>
  18.     <th>Moving From:</th>
  19.     <th>Moving To:</th>
  20.     <th>Date:</th>
  21.   </tr>";
  22.  
  23.   while($row = mysql_fetch_array($result)){
  24.     echo "<tr>";
  25.     echo "<td>" . $row['Name'] . "</td>";
  26.     echo "<td>" . $row['Surname'] . "</td>";
  27.     echo "<td>" . $row['Number'] . "</td>";
  28.     echo "<td>" . $row['Email'] . "</td>";
  29.     echo "<td>" . $row['MovingFrom'] . "</td>";
  30.     echo "<td>" . $row['MovingTo'] . "</td>";
  31.     echo "<td>" . $row['Date'] . "</td>";
  32.     echo "</tr>";
  33.   }
  34.   echo "</table>";
  35.   mysql_close($con);
  36.   ";
  37.   $headers = "From: someone@example.com";
  38.   mail($to,$subject,$body,$headers);
  39.   echo "Mail sent to $to";
  40. ?>
Apr 20 '10 #3
Atli
5,058 Expert 4TB
The problem there is that you need to construct the HTML for the body, not just wrap all the old echo statements in a string.

That is; instead of doing:
Expand|Select|Wrap|Line Numbers
  1. echo "<table>";
  2. echo "<tr><td>...";
Do:
Expand|Select|Wrap|Line Numbers
  1. $body = "<table>";
  2. $body .= "<tr><td>...";
And note the period before the = in the second line. That tells PHP to add the following string to what the variable is storing. If you leave it out, PHP will replace it, which we obviously do not want.

P.S.
Please use [code] tags when posting code examples.

[code] ... Code goes here ... [/code]

Thanks.
Apr 20 '10 #4
Sorry for not putting the code tags. Will do from now on.
Once again thanks for your prompt response - I tried adding as you instructed - see below? Apologies for being such a noob

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $con = mysql_connect("","","");
  3. if (!$con)
  4.   {
  5.   die('Could not connect: ' . mysql_error());
  6.   }
  7.   mysql_select_db("", $con);
  8.   $result = mysql_query("SELECT * FROM contactdetails");
  9.   $to = "";
  10.   $subject = "Results from query";
  11.   $body = "<table border='1'>;
  12.   $body .=<tr>
  13.   $body .=<th>Name:</th>
  14.   $body .=<th>Surname:</th>
  15.   $body .=<th>Number:</th>
  16.   $body .=<th>Email:</th>
  17.   $body .=<th>Moving From:</th>
  18.   $body .=<th>Moving To:</th>
  19.   $body .=<th>Date:</th>
  20.   $body .=</tr>";
  21.  while($row = mysql_fetch_array($result)){
  22.   $body .="<tr>";
  23.   $body .=<td>" . $row['Name'] . "</td>";
  24.   $body .=<td>" . $row['Surname'] . "</td>";
  25.   $body .=<td>" . $row['Number'] . "</td>";
  26.   $body .=<td>" . $row['Email'] . "</td>";
  27.   $body .=<td>" . $row['MovingFrom'] . "</td>";
  28.   $body .=<td>" . $row['MovingTo'] . "</td>";
  29.   $body .=<td>" . $row['Date'] . "</td>";
  30.   $body .=</tr>";
  31.   }
  32.   $body .="</table>";
  33.   mysql_close($con);
  34.   ";
  35.   $headers = "From: someone@example.com";
  36.   mail($to,$subject,$body,$headers);
  37.   echo "Mail sent to $to";
  38. ?>
  39.  
it gives me an error on line 23 which is the following line:
Expand|Select|Wrap|Line Numbers
  1.   $body .=<td>" . $row['Name'] . "</td>";
  2.  
Apr 20 '10 #5
Atli
5,058 Expert 4TB
You left out the starting quote-mark. - All strings must be quoted, or PHP will try to parse them as a PHP command, which will fail with a Parse error.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // This:
  3. $body .=<td>" . $row['Name'] . "</td>";
  4.  
  5. // Needs to be:
  6. $body .= "<td>" . $row['Name'] . "</td>";
  7.  
  8. // Or, even:
  9. $body .= "<td>{$row['Name']}</td>";
  10. ?>
Apr 20 '10 #6
Thank you - That has now sent the email.
The format that it came out in is another story completely though.
I guess I need to change the headers so that it sends html mail .... let me see if i can find out how to do that. Thank you for your help
Apr 20 '10 #7
Figured it out - thank you!
Ok last one - how do I make this form only retrieve NEW data from the database?
Apr 20 '10 #8
Atli
5,058 Expert 4TB
Define: "new data".

Do you mean data within a certain range from the current date, or do you mean data that has not been displayed yet?
Apr 20 '10 #9
I only want the script to pull through data that is new to the table.
The end result is that I would like a cron to run this script 3 times a day and automatically send the NEW data to a specified email address.
Apr 20 '10 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Sylvie Stone | last post by:
Hi All - I have created a survey and I would like the results of the survey to be emailed the boss in *plain text*. Can someone PLEASE take a...
2
by: James | last post by:
Is there any simple way of doing this? I want to redirect my end users to a "results" page that lists the details of their order, but I also want...
2
by: serendipity | last post by:
Hi, I'm not sure if this is possible as i've googled everywhere, but i have a select query that returns a customer record with their associated...
5
by: BaWork | last post by:
I have a web form where a client can select which site members to send an email to. This form is populated from the contents of the member table,...
1
by: Ken Barz | last post by:
Hi, I'm working on a program that will periodically run a query on a SQL database. What I would like it to do is to automatically take the...
0
by: The Programmer | last post by:
Assistance needed urgently! I use a Sybase Database with SQL Anywhere 9 and need to send the SQL query result as the body of the e-mail from...
0
by: The Programmer | last post by:
Assistance needed urgently! I use a Sybase Database with SQL Anywhere 9 and need to send the SQL query result as the body of the e-mail from...
0
by: krouxsa | last post by:
Hi There people... Please help me with this query. I want to send 1 email to multiple email addresses stored in a database. Will it be...
13
by: allan6874 | last post by:
Hello, Can someone please provide me with the code that will allow me to send the results of formmail fields to web page as oppose to sending the...
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.