lional Posted November 21, 2015 Share Posted November 21, 2015 Hi I am trying to write a script where each mail body has the clients name after the greeting $result_first = mysqli_query($conn,"SELECT * from clients WHERE profmed = '1' AND email != ''"); while ($row_first = mysqli_fetch_assoc($result_first)){ $surname_out = $row_first['surname']; $email_out = $row_first['email']; $firstname_out = $row_first['firstname']; $known_as_out = $row_first['known_as']; /*print $known_as_out;*/ $complete_name = $firstname_out . ' ' . $surname_out; $final_msg = $greeting_in_out . ' ' . $known_as_out . '<br /><br />' . $textToStore; $mail->Subject = $subject_out; $mail->IsHTML(true); $mail->Body = " <html> <head> <title></title> </head> <body> <table><tr><td> $final_msg $mail_sig</table></body></html>"; A quick explanation. The client sends through the greeting that he requires to send which is the variable $greeting_in_out, the persons name $known_as_out and the message ($textToStore) is appended to it. But $mail->Body only takes the last name pulled from the database. How can each mail have the persons name on the message Thank you Lional Quote Link to comment Share on other sites More sharing options...
crazycoder Posted November 23, 2015 Share Posted November 23, 2015 Whether table field has value in it? Quote Link to comment Share on other sites More sharing options...
lional Posted November 23, 2015 Author Share Posted November 23, 2015 Morning, At the top of the message I want to say Good Morning, John for example If the table field does not have a value in it then it should just say Good Morning However what is happening is it is attaching the last name in the Database to all the emails, so if Brian is the last name in the database table all the mails will day Good Morning, Brian instead of that persons individual name Thanks Lional Quote Link to comment Share on other sites More sharing options...
crazycoder Posted November 23, 2015 Share Posted November 23, 2015 Did you try printing the $row_first values using print_r($row_first) Quote Link to comment Share on other sites More sharing options...
lional Posted November 23, 2015 Author Share Posted November 23, 2015 No, I am using PHP mailer to send the mail. If I print the variable to screen before PHPmailer assigns the array it is correct, it is just when the array is done it only takes the last name Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted November 23, 2015 Share Posted November 23, 2015 You obviously have only one PHPMailer instance, and it sounds like you just keep overwriting the messages without ever sending them. That of course doesn't make sense. I suggest you create a fresh mailer instance in every iteration, assemble the message and then immediately send it. Quote Link to comment Share on other sites More sharing options...
Stefany93 Posted November 23, 2015 Share Posted November 23, 2015 (edited) Also, this is cleaner: while ($row_first = mysqli_fetch_assoc($result_first)){ extract($resut_first); // Your other code here } That way the keys of the assoc array will become variable names without needing to set them each time with $var = $result_first['var']; The extract function is awesome Edited November 23, 2015 by Stefany93 Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted November 23, 2015 Share Posted November 23, 2015 The extract function is awesome I have to disagree. The extract() function with the default flags is actually harmful, because it blindly overwrites any variable that happens to have the same name as one of the array keys. This is perfect for causing nasty bugs and infinite confusion, but you wouldn't want this in your application. When you apply extract() to any kind of user-controlled data, you even end up with a massive security vulnerability, because now it's possible to actively inject variables and manipulate the control flow of the program. See the discussion about the ancient “Register Globals” misfeature (in a sense, extract() is even worse than that). Don't randomly import variables into the symbol table. Just use $row_variable['fieldname']. Yes, that's a few character more than just $fieldname, but it's much safer. And if you don't like typing, use an IDE with autocomplete. Quote Link to comment Share on other sites More sharing options...
maxxd Posted November 24, 2015 Share Posted November 24, 2015 The extract function is awesome No, no, no, nononononononono ... Nope! Beyond what Jaques1 points out, using extract() can easily lead to messy, unreadable code. Where did all those variables come from, and how can you validate the data you're getting if you're not explicitly stating what you expect? extract() is one of a handful of functions I wish was never included in PHP to begin with. Quote Link to comment 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.