Adam.A Posted December 10, 2008 Share Posted December 10, 2008 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 = "mail@mail.net.au"; $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 Quote Link to comment https://forums.phpfreaks.com/topic/136334-easy-fix-emailing-mysql-query/ Share on other sites More sharing options...
Adam.A Posted December 13, 2008 Author Share Posted December 13, 2008 bump Quote Link to comment https://forums.phpfreaks.com/topic/136334-easy-fix-emailing-mysql-query/#findComment-714353 Share on other sites More sharing options...
premiso Posted December 13, 2008 Share Posted December 13, 2008 $names = ""; while ($row = mysql_fetch_array($sql_result)) { $names .= $row['value']; echo "<br />"; } You have to use the .= to add to the value of a string. Quote Link to comment https://forums.phpfreaks.com/topic/136334-easy-fix-emailing-mysql-query/#findComment-714354 Share on other sites More sharing options...
Adam.A Posted December 13, 2008 Author Share Posted December 13, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/136334-easy-fix-emailing-mysql-query/#findComment-714381 Share on other sites More sharing options...
xtopolis Posted December 13, 2008 Share Posted December 13, 2008 <?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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/136334-easy-fix-emailing-mysql-query/#findComment-714384 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.