Keeping the format of a text field in a MySQL database...

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Cynical Drunk
Forum Newbie
Posts: 1
Joined: Sun May 05, 2002 3:49 pm

Keeping the format of a text field in a MySQL database...

Post by Cynical Drunk »

Hey all,

I've been trying to solve this one problem I'm having with a form and a MySQL database.

When a user enters information in a 'textarea' with physical wrap on my form, all of the carriage returns are lost producing one very, very long line of text when the info is recalled from the db.

I've done a bit of searching on the net and came across the command: $string = nl2br ($string); ... but this doesn't do anything (I'm probably using this command incorrectly). Should I use this command before I shoot the info onto the db or should this command be used when retrieving the text? Will this command even solve the problem?

Essentially, I want to store information in a format which resembles messages stored on a forum.

Any suggestions?
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

After you get the string from the database, you do something like this:

Code: Select all

$string = nl2br($string);

echo $string;
That will turn all carriage returns into <br />'s
Post Reply