Jump to content


This topic is now archived and is closed to further replies.


Pulling variables and \n out of a database...

Recommended Posts

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:

will send this email:


Now, 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...)

This is what arrives in my mailbox


I 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?



Edit:  I just realized the syntax for mail() all wrong... but hopefully you guys get the idea... thanks again!

Share this post

Link to post
Share on other sites
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".

Share this post

Link to post
Share on other sites
I think you need to use eval() in order to parse vars pulled from a database. Be careful when using that function though!

Share this post

Link to post
Share on other sites
When storing/inserting them, make sure you have double quotes and not single quotes around your string.

Share this post

Link to post
Share on other sites
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.

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.