airpirate Posted January 22, 2009 Share Posted January 22, 2009 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 More sharing options...
bobleny Posted January 22, 2009 Share Posted January 22, 2009 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 https://forums.phpfreaks.com/topic/141980-email-results-of-a-php-query/#findComment-743395 Share on other sites More sharing options...
premiso Posted January 22, 2009 Share Posted January 22, 2009 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 https://forums.phpfreaks.com/topic/141980-email-results-of-a-php-query/#findComment-743401 Share on other sites More sharing options...
airpirate Posted January 22, 2009 Author Share Posted January 22, 2009 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 Link to comment https://forums.phpfreaks.com/topic/141980-email-results-of-a-php-query/#findComment-743422 Share on other sites More sharing options...
bobleny Posted January 22, 2009 Share Posted January 22, 2009 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 />"; } ?> Link to comment https://forums.phpfreaks.com/topic/141980-email-results-of-a-php-query/#findComment-743446 Share on other sites More sharing options...
airpirate Posted January 22, 2009 Author Share Posted January 22, 2009 I got a Parse error PHP Parse error: syntax error, unexpected T_FOR in /home/airpirate/email.php on line 44 line 44 is for($j = 0; $j < $num) Link to comment https://forums.phpfreaks.com/topic/141980-email-results-of-a-php-query/#findComment-743459 Share on other sites More sharing options...
bobleny Posted January 22, 2009 Share Posted January 22, 2009 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 />"; } ?> Link to comment https://forums.phpfreaks.com/topic/141980-email-results-of-a-php-query/#findComment-743478 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.