Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Kal
    Kal is offline
    Regular Coder
    Join Date
    Dec 2005
    Posts
    309
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Email Query Results

    Hi Guys,

    I'm having trouble emailing the results of a query.

    For example if I run the following query, I would get a list of status's and a frequency's.

    PHP Code:
    SELECT DISTINCT statusCOUNT(status) AS frequency FROM status GROUP BY status 
    For each row generated how can I email the results.

    Any help would be great.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,863
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Kal
    Kal is offline
    Regular Coder
    Join Date
    Dec 2005
    Posts
    309
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry I should have mentioned I know how to use the mail function, but I'm not sure how to loop through each row generated to be listed in an email when I send it.

    I have the following, but when I run this it emails each row seperately, how can I send them all in one go?

    PHP Code:
    <?php

                        
    require('database.php');
                                
                        
    $query "SELECT DISTINCT status, COUNT(status) AS frequency FROM status GROUP BY status";
                                
                        
    $result mysql_query ($query) or die ('Your query did not match any results: ' mysql_error());

                        if (
    mysql_num_rows($result)>0
                    
                            {                    

                                while(
    $row=mysql_fetch_array($result))
                            
                                    {
                                                    
                                        
    $to 'xx@xxx.xx.xx';
                        
    $subject 'Report';
                        
                        
    $body '<HTML>';

                            
    $body $body '<HEAD>';

                            
    $body $body '</HEAD>';

                            
    $body $body '<BODY>';

                                
    $body $body '<div width="500">';

                                    
    $body $body '<table border="0" cellspacing="2" cellpadding="2">';
                                        
                                        
    $body $body '<tr>';
                                            
    $body $body '<td>';
                                                
    $body $body $row['status'];
                                            
    $body $body '</td>';
                                            
    $body $body '<td>';
                                                
    $body $body ' - ';
                                            
    $body $body '</td>';
                                            
    $body $body '<td>';
                                                
    $body $body $row['frequency'];
                                            
    $body $body '</td>';
                                        
    $body $body '<tr>';

                                    
    $body $body '</table>';

                                
    $body $body '</div>';

                            
    $body $body '</BODY>';

                        
    $body $body '</HTML>';

                        
    $headers "MIME-Version: 1.0\r\n"."Content-type: text/html; charset=iso-8859-1\r\n"."From: noreply@xxx.xxx"
                        
                        if(
    $result)
                            {
                                
    mail($to$subject$body$headers);
                            }
                        else
                            {
                                
    '';
                            }

                                    }
                    
                            }

                    
    ?>

  • #4
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    PHP Code:
    //Just wanted to let you see this real quick.
    $body "this is a string of text";
    $body $body " this is another string";
    echo 
    $body;

    //is the same as saying

    $body2 "this is a string of text";
    $body2 .= " this is another string";
    echo 
    $body2

    You should take everything out of your loop except this part.

    PHP Code:
                                        $body $body '<tr>'
                                            
    $body $body '<td>'
                                                
    $body $body $row['status']; 
                                            
    $body $body '</td>'
                                            
    $body $body '<td>'
                                                
    $body $body ' - '
                                            
    $body $body '</td>'
                                            
    $body $body '<td>'
                                                
    $body $body $row['frequency']; 
                                            
    $body $body '</td>'
                                        
    $body $body '<tr>'
    Just let the above part run build inside the loop then you'll finish building the HTML and email it later on in the script.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •