www.webdeveloper.com
Results 1 to 15 of 15

Thread: [RESOLVED] Send a MySQL row via email

  1. #1
    Join Date
    Aug 2009
    Posts
    53

    resolved [RESOLVED] Send a MySQL row via email

    I'm building a feedback system & I'm trying to get emailing to work.

    What I'm trying to do:
    1- I press the Mail icon on the comment I want emailed.
    2- The comment gets emailed to me.

    What the problem is, I can get it to send successfully with everything but the body working. Here's my code:
    PHP Code:
    <?php
    include('connect.php');
    $query "SELECT message from visitordata where id='".$id."'";

    $to "eatthelizard@gmail.com";
    $subject "Your feedback, sir.";
    $body "Here's the feedback you requested:<br />".mysql_query($query)."";
    mail($to$subject$body);
    ?>
    * the $id is set in by mail.php?id=xx. The id is of the comment I chose.
    Here's the body of what it comes out as:
    "Here's the feedback you requested:<br />Resource id #7"

    How can I get this to work?

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,633
    You'll have to use one of the mysql_fetch_*() functions to fetch a result row from the query result set.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jan 2006
    Location
    MN
    Posts
    440
    One way;

    PHP Code:
    $result mysql_query($query,$link);

    while (
    $a_row mysql_fetch_array($result){
    $message $a_row['message'];
    }

    mail ($to$subject$message$headers); 
    It is also likely that the message will need formatting before it can be sent.

  4. #4
    Join Date
    Aug 2009
    Posts
    53
    I changed my code to this:

    PHP Code:
    <?php
    include('connect.php');
    $query "SELECT message from visitordata where id='".$id."'";
    $to "eatthelizard@gmail.com";
    $subject "Your feedback, sir.";


    $result mysql_query($query,$db); 

    while (
    $a_row mysql_fetch_array($result)) { 
    $message $a_row['message']; 


    mail ($to$subject$message$headers);
    ?>
    But it didn't work. I got a blank email. Any suggestions?

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,633
    First check to see if $result is false after the query is executed, and if so die() or otherwise echo/log the error and the query to debug it. If not false, check to see if mysql_num_rows() is > 0. If not, again log/repot the query string to see if it looks correct, and test it by directly entering it into the MySQL command line interface to verify that your query/logic is correct. Ultimately you'll want some sort of defensive coding approach like:
    PHP Code:
    $result mysql_query($query);
    if(
    $result)
    {
       if(
    mysql_num_rows($result))
       {
          
    $message mysql_result($result0);
          if(
    mail($to$subject$message$headers))
          {
             
    // success message here
          
    }
          else
          {
             echo 
    "Sorry, there was a problem sending the email.";
          }
       }
       else
       {
          
    error_log("No row returned: $query");
          echo 
    "Sorry, no matching data was found, email not sent.";
       }
    }
    else
    {
       
    error_log(mysql_error()."\n".$query);
       echo 
    "Sorry, there was a problem with the database--email not sent.";

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  6. #6
    Join Date
    Aug 2009
    Posts
    53
    I'm getting the "Sorry, no matching data was found, email not sent." error. What does this mean?

  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,633
    It means that your query did not return any matching rows from the database. Check the error log to see what the query looked like, or change error_log() to die() to display it to the browser (assuming error reporting settings allow it). At a guess, $id might be empty -- where does it get set?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  8. #8
    Join Date
    Aug 2009
    Posts
    53
    The id is 32 which is in the database & it is set in the URL ie: mail.php?id=32

  9. #9
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,633
    Any difference if you get rid of the quotes around the value in the query?
    PHP Code:
    $query "SELECT message from visitordata where id=" . (int) $id
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  10. #10
    Join Date
    Aug 2009
    Posts
    53
    Still get the same error.

    I changed error_log() to die() & got:
    No row returned: SELECT message from visitordata where id=0
    Last edited by mattwidge; 10-23-2009 at 02:50 AM.

  11. #11
    Join Date
    Aug 2009
    Posts
    53
    Sorry for double post, but I put in
    PHP Code:
    echo $id
    & got a blank page. I then tried
    PHP Code:
    echo "".$id.""
    & still got a blank page.

    I don't think the id variable is being set.

  12. #12
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,633
    It's certainly not explicitly set in the code you've provided. Where is it supposed to come from?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  13. #13
    Join Date
    Aug 2009
    Posts
    53
    From a file called display.php.

    A list of MySQL rows is displayed & there is a link specific to each row that goes to the mail.php file. The link works like this:
    PHP Code:
    <a href=\"mail.php?id=".$r['id']."\"><img src=\"email.png\" border=\"0\"></a> 
    that is how the id is set. When it gets to mail.php the URL is mail.php?id=*row number*

    If you need any thing else, let me know.

  14. #14
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,633
    How about using $_GET['id'] instead of $id. ($id would only work if register_globals is enabled, and since that "feature" is deprecated, you should not depend upon it in any case.)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  15. #15
    Join Date
    Aug 2009
    Posts
    53
    SUCCESS!!!!!!

    Here it is:
    PHP Code:
    <?php
    include('connect.php');
    $query "SELECT message FROM visitordata WHERE id='".$_GET['id']."'"
    $to "eatthelizard@gmail.com";
    $subject "Your feedback, sir.";

    $result mysql_query($query);
    if(
    $result)
    {
       if(
    mysql_num_rows($result))
       {
          
    $message mysql_result($result0);
          if(
    mail($to$subject$message$headers))
          {
             echo 
    "IT WORKED!!!!";
          }
          else
          {
             echo 
    "Sorry, there was a problem sending the email.";
          }
       }
       else
       {
          echo 
    "Sorry, no matching data was found, email not sent.<br>";
          die(
    "No row returned: $query");
       }
    }
    else
    {
       echo 
    "Sorry, there was a problem with the database--email not sent.<br>";
       die(
    mysql_error()."\n".$query);
    }
    ?>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles