saving paragraphs in multiline to mysql

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
steven121
Forum Newbie
Posts: 2
Joined: Sun Nov 22, 2009 3:57 pm

saving paragraphs in multiline to mysql

Post by steven121 »

This might seem like a really silly question but how can i keep the format of posted text into a mysql database. e.g. i have a multiline textarea as part of a form. When i post this form to my php script I am saving all the data into the mysql database. However if the user makes carrage returns or copies text into the textarea that has paragraphs, I want to keep these paragraph format and saved into the database so i can echo out to a page and retain the format.
User avatar
iankent
Forum Contributor
Posts: 333
Joined: Mon Nov 16, 2009 4:23 pm
Location: Wales, United Kingdom

Re: saving paragraphs in multiline to mysql

Post by iankent »

it should by default. a newline is represented by \n in a string, and that should be retained when you write it to the database.

if you output it as HTML then it'll look as though its been lost, as \n is ignored as a new line in HTML. you'd need to replace new lines with <br /> using something like str_replace. if you output it inside another textarea then you should see the newlines intact

hth
steven121
Forum Newbie
Posts: 2
Joined: Sun Nov 22, 2009 3:57 pm

Re: saving paragraphs in multiline to mysql

Post by steven121 »

thanks works a treat i used the nl2br function
User avatar
iankent
Forum Contributor
Posts: 333
Joined: Mon Nov 16, 2009 4:23 pm
Location: Wales, United Kingdom

Re: saving paragraphs in multiline to mysql

Post by iankent »

steven121 wrote:i used the nl2br function
for some reason I always forget that exists, don't know why... :banghead:
Post Reply