Jump to content

Archived

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

colinexl

How do I store formatted text into MySQL?

Recommended Posts

Hi all, I have a message board that will store user input into a mysql database. The input is much like a forum input like the one i'm typing in now. It's able to store line returns etc. I'm using a LONGTEXT to store the user inputs but when I print it back using PHP, it gives one long time of text. For example if the user typed:

Hi,

This is a test.

Bye.

It would print out:

Hi, This is a test. Bye.

I'm submitting the user input into the database using POST method. If I do a manual "select * from..." in the mysql CLI, I can actually see the line breaks in the result. It's just that when PHP prints it, it leaves out the line breaks. Any help would be grateful! Thank!

Share this post


Link to post
Share on other sites
Use the nl2br() function when you send the text to the browser for displaying. (http://www.php.net/nl2br)

Ken

Share this post


Link to post
Share on other sites
When ever I get text from a Mysql Table
I use
$text = nl2br(stripslashes($text));

If you use mysql_real_escape_string or simular like you should, you would end up with slashes that need to be removed

Share this post


Link to post
Share on other sites
Whenever I use mysql_real_escape_string() when I insert data, I don't have to use stripslashes() when I pull the data out. If you look at the data in the database, it's stored without the slashes.

Ken

Share this post


Link to post
Share on other sites
not true with my database

if I have a string
$string = "Let's go to a party";

then the ' is escaped

Share this post


Link to post
Share on other sites
[quote]not true with my database[/quote]

You probably have magic_quotes_gpc turned on, and ken probably does not, which is a function of php not the database.

Share this post


Link to post
Share on other sites
I have magic_quotes_gpc turned [b]on[/b] and magic_quotes_runtime turned [b]off[/b]. It's the second setting that is affecting pulling data from the database (I believe).

Ken

Share this post


Link to post
Share on other sites
I was unfamiliar with that (magic_quotes_runtime) until just now...here's what the manual says about it:

[quote]magic_quotes_runtime  boolean

    If magic_quotes_runtime is enabled, most functions that return data from any sort of external source including databases and text files will have quotes escaped with a backslash. If magic_quotes_sybase is also on, a single-quote is escaped with a single-quote instead of a backslash. [/quote]

Share this post


Link to post
Share on other sites
All i know, when I use <textarea>
They come back with slashes around single quotes
but all that matters at the end of the day, is that the script using works
using strip_slashes where there are no slashes does not do any harm

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.