Jump to content


Photo

How do I store formatted text into MySQL?


  • Please log in to reply
9 replies to this topic

#1 colinexl

colinexl
  • Members
  • Pip
  • Newbie
  • 8 posts

Posted 03 August 2006 - 12:45 AM

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!

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 03 August 2006 - 12:47 AM

Use the nl2br() function when you send the text to the browser for displaying. (http://www.php.net/nl2br)

Ken

#3 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 03 August 2006 - 12:51 AM

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
Tell me the problem, I will try tell you the solution

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 03 August 2006 - 01:16 AM

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

#5 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 03 August 2006 - 01:19 AM

not true with my database

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

then the ' is escaped


Tell me the problem, I will try tell you the solution

#6 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 03 August 2006 - 01:20 AM

not true with my database


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

#7 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 03 August 2006 - 01:25 AM

I have magic_quotes_gpc turned on and magic_quotes_runtime turned off. It's the second setting that is affecting pulling data from the database (I believe).

Ken

#8 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 03 August 2006 - 01:30 AM

I was unfamiliar with that (magic_quotes_runtime) until just now...here's what the manual says about it:

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.



#9 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 03 August 2006 - 01:32 AM

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
Tell me the problem, I will try tell you the solution

#10 colinexl

colinexl
  • Members
  • Pip
  • Newbie
  • 8 posts

Posted 03 August 2006 - 02:03 AM

Hey thanks for the replies. I will give it a try soon =)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users