Jump to content

Easy Fix -> Emailing mySQL query


Adam.A

Recommended Posts

Hi all,

 

I have a problem with the following, I am trying to insert a query from my mySQL data base into the php "email" function

 

 

// mySQL Connect String  
$connection = mysql_connect("$server","$username","$password");  
  
// Select the database  
mysql_select_db("$dbname");  

  
$sql = 'SELECT * FROM `jos_facileforms_subrecords` LIMIT 0, 30 '; 

// execute SQL query and get result  
$sql_result = mysql_query($sql,$connection)  
     or die(mysql_error());   
  

// Loop through the data set and extract each row in to it's own variable set  
  
while ($row = mysql_fetch_array($sql_result)) {  
    
	$names = $row['value'];
echo "<br />";

}  



$to = "[email protected]";
$subject = "Hi2u!";
$body = "$names";



if (mail($to, $subject, $body)) {
  echo("<p>Message successfully sent!</p>");
} else {
  echo("<p>Message delivery failed...</p>");
}


?>

 

Results:

It is only emailing returning 1 record but if i just use the echo function it displays all to the web page.

 

Required solution:

I would like to email all the results from the query i can see how the while statement is looping through the data and formatting it but i am at a loss on how to store this in an array(?) and parse it into the email function.

 

Any direction/assistance on this would be much appreciated as i have scoured the web and this forum and not had any luck.

 

-Adam

 

Link to comment
https://forums.phpfreaks.com/topic/136334-easy-fix-emailing-mysql-query/
Share on other sites

Hi, thanks for your response it worked perfectly

 

is it possible to return the formatting as well, because i need to represent it as one record per line e.g.

 

record1

record2

record3

 

I think we will have to use a function because i tired simply adding another string variable to the

 echo "<br />";

but all that dose it make the formatting get placed after the string

 

any thoughts

-Adam

<?php
$names = '';
while ($row = mysql_fetch_array($sql_result)) {
   
  $names .= $row['value'] . "\n";

} 
?>

I think that's the proper Email way.

 

Also, if you did want it as an array like you stated above:

<?php
$names = array();
while ($row = mysql_fetch_array($sql_result)) {
   
  $names[] = $row['value'];

}

//... later can append it like

$body = implode("\n",$names);
?>

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.