Jump to content


Photo

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


  • Please log in to reply
5 replies to this topic

#1 ssmK

ssmK
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 19 October 2006 - 10:32 PM

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:

mail("Line1\nLine2\nLine3");
will send this email:

Line1
Line2
Line3

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

mail($row[3]);
This is what arrives in my mailbox

Line1\nLine2\nLine3

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?

Thanks!

Rob

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

#2 btherl

btherl
  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 20 October 2006 - 03:23 AM

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:

$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')";

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".

#3 kalivos

kalivos
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts
  • LocationCalifornia

Posted 20 October 2006 - 03:33 AM

I think you need to use eval() in order to parse vars pulled from a database. Be careful when using that function though!
Make a donation to phpfreaks. They need your help![br][br]

#4 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 20 October 2006 - 07:29 AM

When storing/inserting them, make sure you have double quotes and not single quotes around your string.

#5 ssmK

ssmK
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 20 October 2006 - 02:45 PM

Thanks, guys! I'll give this a try and report back.

#6 ssmK

ssmK
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 20 October 2006 - 03:52 PM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users