Page 1 of 1

block of text

Posted: Sat Apr 08, 2006 5:51 am
by turgs
what data type should I use for mySQL for Text Blocks like this area I'm typing in right now...???

on which, the format of the text will be preserve like carriage returns...

is varchar just alright?

Posted: Sat Apr 08, 2006 7:02 am
by Oren
For your first question the answer is: No, varchar will be fine for strings no longer than 255 chars!
I guess you want to be able to store strings longer than 255 chars... for that, use TEXT (up to 65535 chars).
If you need more than 65535 chars check out this: http://htmlite.com/mysql003.php

And for your second question... I really can't understand your question, can you please try to explain it better? (or perhaps I'm the only one who can't understand it? :?)

Posted: Sat Apr 08, 2006 8:03 am
by feyd
All of the TEXT based types in MySQL preserve the formatting, technically. To display them as such normally involves nl2br() and replacing all leading whitespace with   pairs.

Posted: Sat Apr 08, 2006 9:03 am
by Oren
Oh... now I get you. feyd is right.
Let's say you read some data from the db and put in: $data
Use this code:

Code: Select all

<?php

	$data = htmlspecialchars($data, ENT_QUOTES);
	$data = str_replace(' ', '&nbsp;', $data);
	$data = nl2br($data);

	echo $data;
?>
Make sure to call the functions in this specific order, it is extremely important or otherwise the date won't be shown correctly!