ssmK Posted October 19, 2006 Share Posted October 19, 2006 Hi there, I don't mean to pester you guys with these questions, I usually try to figure this stuff out myself, but this one has me stumped. I'm making an automated email system for communicating with customers, and have got the mail() function working well. And, I'd like to store the contents, subjects for different types of email in a MySQL database.Here's my problem:[code]mail("Line1\nLine2\nLine3");[/code]will send this email:Line1Line2Line3Now, say I store 'Line1\nLine2\nLine3' as a TEXT, and then do something like this (assuming, of course, that I've pulled what I need to into $row...)[code]mail($row[3]);[/code]This is what arrives in my mailboxLine1\nLine2\nLine3I am also having the same problem with variables. I can not figure out how to store something like this (stored in MySQL as TEXT) 'Hi, $first_name, thanks...' and then have it be of any use to me without str_replace(). I'm guessing I need something of a reverse escape string function, or something that will tell PHP to re-parse the string. Anyone know how to do this?Thanks!RobEdit: I just realized the syntax for mail() all wrong... but hopefully you guys get the idea... thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/24496-pulling-variables-and-n-out-of-a-database/ Share on other sites More sharing options...
btherl Posted October 20, 2006 Share Posted October 20, 2006 The purpose of this forum is to pester us with questions :) Then we get a warm fuzzy feeling when we can help!I'm guessing the problem here is that you are putting a literal string 'Line1\nLine2\nLine3' into the database.. notice the single quotes there. "\n" is only converted to a linefeed if it's within double quotes. So, this should work:[code]$content = "Line1\nLine2\nLine3"; # Double quotes means \n is converted to linefeed$escaped_content = mysql_real_escape_string($content);$query = "INSERT INTO table (content) VALUES ('$escaped_content')";[/code]Try that query and fetch the data back and see if it works. The key point is that by setting $content using double quotes around the \n characters, the \n are all converted to linefeeds. After that things should be fine.Actually you could use str_replace .. str_replace('\n', "\n", $string). It converts literal '\' and 'n' into the linefeed character "\n". Quote Link to comment https://forums.phpfreaks.com/topic/24496-pulling-variables-and-n-out-of-a-database/#findComment-111661 Share on other sites More sharing options...
kalivos Posted October 20, 2006 Share Posted October 20, 2006 I think you need to use eval() in order to parse vars pulled from a database. Be careful when using that function though! Quote Link to comment https://forums.phpfreaks.com/topic/24496-pulling-variables-and-n-out-of-a-database/#findComment-111668 Share on other sites More sharing options...
Daniel0 Posted October 20, 2006 Share Posted October 20, 2006 When storing/inserting them, make sure you have double quotes and not single quotes around your string. Quote Link to comment https://forums.phpfreaks.com/topic/24496-pulling-variables-and-n-out-of-a-database/#findComment-111705 Share on other sites More sharing options...
ssmK Posted October 20, 2006 Author Share Posted October 20, 2006 Thanks, guys! I'll give this a try and report back. Quote Link to comment https://forums.phpfreaks.com/topic/24496-pulling-variables-and-n-out-of-a-database/#findComment-111854 Share on other sites More sharing options...
ssmK Posted October 20, 2006 Author Share Posted October 20, 2006 Thanks to btherl and Daniel0, that solution worked, I was unaware that PHP and MySQL shared the ' ' vs " " trait. And thanks to kalivos for the eval() heads up! That worked as well. Quote Link to comment https://forums.phpfreaks.com/topic/24496-pulling-variables-and-n-out-of-a-database/#findComment-111897 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.