Storing formatted text in mysql Db for echo statement
Posted: Thu Nov 13, 2008 4:46 pm
I am new at using PHP and MySQL databases, so kindly bear with me if my questions are overtly simple. I greatly appreciate any help with the following, as forum searches and online references have not given me a direct answer.
I am trying to store a formatted text paragraph in a MySQL database longtext field. I would like to use the fields value in an echo statement to display the paragraph on a web page.
Despite multiple different approaches, my stored text is never displayed with the formatting intact. I assume this problem has two parts. First the formatting of the text I am pasting into my longtext field (I am using navicat to visually edit the mysql tables/records). Secondly, my php code to dynamically display the lontext field's value may be incorrect.
To address my first question:
I have tried pasting text directly from MS word and also from notepad, as well as html formatted text from Adobe Dreamweaver. None has worked. Can someone confirm the correct text format I must use when storing text using longtext (plain text, etc.) and also whether long text is the correct field type to use? (I will be storing long essays).
To address my second question, I have tried reformatting the field's value upon display using the following code snippets:
None of the above have worked, does anyone know of a way to display stored text while keeping some formatting? I would prefer to be able to store the text with some HTML formatting and line/parapgraph breaks, but will settle for just line/paragraph breaks if this is not possible or simple.
I am trying to store a formatted text paragraph in a MySQL database longtext field. I would like to use the fields value in an echo statement to display the paragraph on a web page.
Despite multiple different approaches, my stored text is never displayed with the formatting intact. I assume this problem has two parts. First the formatting of the text I am pasting into my longtext field (I am using navicat to visually edit the mysql tables/records). Secondly, my php code to dynamically display the lontext field's value may be incorrect.
To address my first question:
I have tried pasting text directly from MS word and also from notepad, as well as html formatted text from Adobe Dreamweaver. None has worked. Can someone confirm the correct text format I must use when storing text using longtext (plain text, etc.) and also whether long text is the correct field type to use? (I will be storing long essays).
To address my second question, I have tried reformatting the field's value upon display using the following code snippets:
Code: Select all
<?php echo $row_Recordset1['Bio']; ?>
<?php print nl2br(str_replace(" "," ",stripslashes($row_Recordset1['Bio']))); ?>
<?php
$text= htmlspecialchars(htmlentities ($row_Recordset1['Bio']));
echo $text;
?>
<?php $text2 = $row_Recordset1['Bio'];
$text2 = str_replace("\n", "<br>", $text2);
echo $text2;
?>