PHP Functions Help

Discussion in 'Web Design and Development' started by SChaput, Dec 20, 2009.

  1. SChaput macrumors regular

    Joined:
    Jul 2, 2008
    Location:
    United States
    #1
    I have just recently discovered the wonderful world of functions to make my life easier. I am trying to create a function that sends an email to certain people from a database.
    Code:
    function sendEmail($email,$random)
    {
    //mysql query here
     
    
    
    while($row = mysql_fetch_array($result))
    {
        $id = $row['id'];
        $random = $row['random'];
    	$email = $row['email'];
    
    
    
    // subject
    $subject = 
    
    // message
    $message = "";
    
    
    mail($email, $subject, $message);
    }
    
    
    }
    
    I call the function in my website like this.
    Code:
      sendEmail($email,$random);
    
    I feel the error lies in where i call the function, it just doesn't look right to me.

    Any help is appreciated. Thanks!
     
  2. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #2
    You aren't assigning anything to $subject, you just have an equal sign with nothing after it. That creates syntax issues. Make sure to view the PHP error logs, which should reveal these problems. Also not sure why you're sending the $email and $random variables to the function, when you just recreate them inside the function.
     
  3. kAoTiX macrumors 6502

    kAoTiX

    Joined:
    Oct 14, 2008
    Location:
    Midlands, UK
    #3
    I think he's using $email and $random to perform some kind of SQL query to return a specific user using the email and random field supplied.

    I agree though, you don't end $subject or even assign it a value.

    If you could provide the actual error message that is being displayed it may be easier to tell if there is a problem somewhere else.
     
  4. savar macrumors 68000

    savar

    Joined:
    Jun 6, 2003
    Location:
    District of Columbia
    #4
    You didn't ask a question!
     
  5. jaikob macrumors 6502

    jaikob

    Joined:
    Jul 1, 2008
    Location:
    Freeland, MI
    #5
    PHP:
    function sendEmail($email,$random)
    {

    //mysql query here
    // I wish you would of posted this but w/e. Just make sure to add or die(mysql_error); at the end....

    do {
    $id $row['id'];
    $random $row['random'];
    $email $row['email'];

    // subject
    $subject ""// I have no clue what you have here...

    // message
    $message ""// And again, no clue.

    mail($email$subject$message);

    } while(
    $row mysql_fetch_array($result));
    }
    return 
    TRUE;
    }
    Tried revising. I think you just missed a semicolon on your while statement. Plus you implemented a do while loop wrong.
     
  6. SChaput thread starter macrumors regular

    Joined:
    Jul 2, 2008
    Location:
    United States
    #6
    Sorry for the delay in response. I got the code working, it was an error before calling the function. For those interested here is the final, <b>working</b> code.
    PHP:
    function sendEmail($email,$random)
    {

     
    //Database connection
        
     
    $query  "SELECT * FROM hmr WHERE email ='$email' AND random = '$random' and verified=0 LIMIT 1 ";
    $result mysql_query($query);


    while(
    $row mysql_fetch_array($result))
    {
        
    $id $row['id'];
        
    $random $row['random'];
        
    $email $row['emaul'];



    // subject
    $subject 'Verification Needed @ I Hate My Roommate';

    // message
    $message "This is an automated message from IHateMyRoommate.net.

    According to our records someone has used this email address, 
    $email, and added a post at our website.  If this was not you, please delete this email.

    However, if you posted the story click the following link to verify your posting on our site, letting us know you are not spam.

    Thank you.  We appreciate it.

    http://www.ihatemyroommate.net/verify.php?id=
    $id&random=$random

    - The I Hate My Roommate Team.
    "
    ;

    $headers  'MIME-Version: 1.0' "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' "\r\n";


    $headers "From: verification@ihatemyroommate.net \r\n" 
               
    "Reply-To: no-reply@ihatemyroommate.net \r\n" 
               
    "X-Mailer: PHP/" phpversion(); 

    mail($email$subject$message$headers);
    }

    }
    This function is called using, sendEmail($email, $random);

    Thanks.
     

Share This Page