Emailing db Content from php

Discussion in 'PHP' started by Canoman5, Mar 26, 2013.

  1. #1
    Hi
    I have a very simple database and one record in a table has 5 fields.
    each record has a field that denotes the record as active.

    I can retrieve the records and display them without a problem.
    I can email the contents of ONE record at a time as they are created using sendmail and the $_POST values.

    What I am struggling with is-
    If multiple records are set as active, how do I create an array of those records and email that array in a legible format?
    Ive spent hours trawling the net reading stuff and am just confusing myself even more. I'm not a programmer but I'm not dumb either, i can follow some simple instructions if anyone has the time to share some!

    ideally i would like to create something that looks like -

    column 1 | Column2 | Column 3 |
    Val1 val2 val3

    etc.
    thanks in advance
    Canoman5
     
    Last edited: Mar 26, 2013
    Canoman5, Mar 26, 2013 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,898
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #2
    the basic mail command is
    mail($email, $subject, $body);
    PHP:
    so if you have info you want to put in the email it's as simple as this
    $sql = "select * from `mytable`";
    $result = mysql_query($sql) or die(mysql_error.'<br />'.$sql);
    $body = '';
    while ($row = mysql_fetch_array($result){
        for($i=0; $i<5; $i++){
            $body .= $row[$i]. "\t";
        }
        $body .= "\n";
    }
    mail($email, $subject, $body);
    PHP:
    you still have to populate the email and subject fields.
     
    sarahk, Mar 26, 2013 IP
  3. Vick.Kumar

    Vick.Kumar Active Member

    Messages:
    138
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    90
    #3
    Here's a possible way, you get the count of records in table, and store in a variable.

    You can query each record for each information you need to be pulled from the table/database and store it using simple PHP arrays, with a key if possible.


    Use a for loop, where $i is less than count of records until it's met or equal to it. Output the data using "/t" for aligning.
     
    Vick.Kumar, Mar 27, 2013 IP
  4. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #4
    I would do what Sarah said adding the field names (would make it easier to read, at least for me it would):

    
    while ($row = mysql_fetch_assoc($result){
        $body .= print_r($row, true) . "\n";
    }
     
       
     
    
    PHP:
     
    ThePHPMaster, Mar 29, 2013 IP