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 = "[email protected]";

$header = "From: [email protected]\n"

  . "Reply-To: [email protected]\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
https://forums.phpfreaks.com/topic/141980-email-results-of-a-php-query/
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....

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.

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

[email protected]

 

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

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 = "[email protected]";
$header = "From: [email protected]\nReply-To: [email protected]\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 />";
}	
?>

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 = "[email protected]";
   $header = "From: [email protected]\nReply-To: [email protected]\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 />";
   }
?>

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.