Jump to content

Email results of a PHP Query


airpirate

Recommended Posts

I need help figuring out a script. I am trying to email the results of a mysql query, the query results have multiple rows. I have been able to get it to return one row in the email that is sent, but I need it to display all the results (multiple rows). Here is what I have so far and where I am stuck;

 

#!/usr/bin/php

#correct usage is ./email.php <table name>

 

<?

 

$date = date('F d, Y');

 

// Connecting, selecting database

$link = mysql_connect('localhost','user','pass');

if (!$link) {

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

}

 

// Connect to the right DB

$db_selected = mysql_select_db('database', $link);

if (!$db_selected) {

  die ('Error Reading Table : ' . mysql_error());

}

 

$table = $argv[1];

 

$result = mysql_query("select email from database.$table;");

$arr = mysql_fetch_array($result);

$result2 = $arr[0];

 

if (!$result) {

    echo "Could not successfully run query against the DB: " . mysql_error();

    exit;

}

 

$email = "test@test.com";

$header = "From: company@company.org\n"

  . "Reply-To: me@me.com\n";

$subject = 'This is the results of the query';

$message = "Query dated: $date \n"

. "$result2 \n"

. "\n";

 

mail($email, $subject, $message, $header);

 

printf("Completed \n");

 

?>

 

Any help would really be appreciated.

 

Thx

Link to comment
Share on other sites

You message variable is screwed up. You have this:

$message = "Query dated: $date \n"
. "$result2 \n"
. "\n";

 

You need this:

$message = "Query dated: ". $date ." \n". $result2;

 

If that dose not work, try simply echoing the message to the screen like this:

echo $message;

 

Then show us the results of the echo....

Link to comment
Share on other sites

You message variable is screwed up. You have this:

$message = "Query dated: $date \n"
. "$result2 \n"
. "\n";

 

You need this:

$message = "Query dated: ". $date ." \n". $result2;

 

If that dose not work, try simply echoing the message to the screen like this:

echo $message;

 

Then show us the results of the echo....

 

Not to mention he is also not looping through the data, look at mysql_fetch_array for examples of how to loop through mysql data.

Link to comment
Share on other sites

I made the changes you suggested and still got the same results, just one record was emailed. So I put in echo($message) and got this back

 

#correct usage is ./email.php table

 

Query dated: January 22, 2009

test@test.orgCompleted

 

Any ideas? I ran my query from the command line and it worked fine, all the records were displayed, just not when I run it through the php script

Link to comment
Share on other sites

Try running this real quick, and show me the results...:

<?php
$date = date('F d, Y');

// Connecting, selecting database
$link = mysql_connect('localhost','user','pass');
if (!$link) {
	die('Could not connect: ' . mysql_error());
}

// Connect to the right DB
$db_selected = mysql_select_db('database', $link);
if (!$db_selected) {
   die ('Error Reading Table : ' . mysql_error());
}

$table = $argv[1];

$result = mysql_query("select `email` from `database". $table ."`");
$arr = mysql_fetch_array($result);
$result2 = $arr[0];

if (!$result) {
	echo "Could not successfully run query against the DB: " . mysql_error();
	exit;
}

$email = "test@test.com";
$header = "From: company@company.org\nReply-To: me@me.com\n";
$subject = 'This is the results of the query';
$message = "Query dated: ". $date ." \n". $result2;

mail($email, $subject, $message, $header);

//printf("Completed \n");

echo "<br /><br /><br />";
$num = mysql_numrows($arr)
for($j = 0; $j < $num)
{
	echo $arr[$j] ."<br />";
}	
?>

Link to comment
Share on other sites

Sorry about that...

<?php
   $date = date('F d, Y');

   // Connecting, selecting database
   $link = mysql_connect('localhost','user','pass');
   if (!$link) {
      die('Could not connect: ' . mysql_error());
   }

   // Connect to the right DB
   $db_selected = mysql_select_db('database', $link);
   if (!$db_selected) {
      die ('Error Reading Table : ' . mysql_error());
   }

   $table = $argv[1];

   $result = mysql_query("select `email` from `database". $table ."`");
   $arr = mysql_fetch_array($result);
   $result2 = $arr[0];

   if (!$result) {
      echo "Could not successfully run query against the DB: " . mysql_error();
      exit;
   }

   $email = "test@test.com";
   $header = "From: company@company.org\nReply-To: me@me.com\n";
   $subject = 'This is the results of the query';
   $message = "Query dated: ". $date ." \n". $result2;

   mail($email, $subject, $message, $header);

   //printf("Completed \n");

   echo "<br /><br /><br />";
   $num = mysql_numrows($arr)
   for($j = 0; $j < $num; $j++)
   {
      echo $arr[$j] ."<br />";
   }
?>

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.