Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1961
  • Last Modified:

Need some help with line breaks in MySQL database.

I created a simple IT helpdesk for my companies Intranet page.  My question is how can I send the text field to the MySQL database and preserve the line breaks?  For example..

If I put some text
in my text box, it

looks like this by hitting
the enter key.

When I display the text field on the records page it all comes out as one paragraph.  If I put <br /> in the text box at each line break then its correct but I dont want my computer illiterate coworks to have to remember to do that for each line break.  Can anyone help?  I am using Dreamweaver CS3 to generate the code if that matters to anyone.

Thanks for any help!
0
graphxdiva
Asked:
graphxdiva
2 Solutions
 
nizsmoDeveloperCommented:
for some variable in php, you can do this:

echo nl2br($my_string);

where $my_string is the string which you want the transformation of newline to <br> to take place.
Try it out, it should be what you are looking for.
0
 
steelseth12Commented:
The new lines are preserved but because you are displaying html you need to put the <br> tag
What you can do is when you output the information instead of doing

echo $mytext;

do

echo nl2br($mytext);

this will convert all line breaks to <br> tags
0
 
nizsmoDeveloperCommented:
As a brief example below to demonstrate how to use the function.
Essentially when the "enter" key is pressed, a newline is inserted and represented by \n.
<?php
echo nl2br("foo isn't\n bar");
?>
 
// outputs foo isn't<br /> bar

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Aleksandar BradarićSoftware DeveloperCommented:
>  If I put <br /> in the text box

You've got a `nl2br()` function for PHP which does that for you. You can also take look at `addslashes()` and `stripslashes()` functions.
0
 
graphxdivaAuthor Commented:
Great nl2br worked.  One last question..  If the person hits enter/return twice to leave an empty line, the form strips the empty line prior to putting it into the DB, how can I stop that so the empty line remains?
0
 
nizsmoDeveloperCommented:
hmm it shouldn't do. it should be stored into the database ok, as it shouldn't strip the empty line (it should preserve it).
0
 
graphxdivaAuthor Commented:
No if I create a new or edit an existing record.. in my text field enter

some

text

it displays

some
text

0
 
nizsmoDeveloperCommented:
i did a quick test, and it preserves this, but i was using the TEXT field type for my database field. Is this what you are using?
0
 
graphxdivaAuthor Commented:
You mean text as your datatype in the table?  I was at medium text, switched to text and same thing.
0
 
graphxdivaAuthor Commented:
Well I created a new db to test it on again and it worked as you said.. Not sure why it didnt on the other but oh well.  Thanks for your help!
0
 
nizsmoDeveloperCommented:
weird thing! no problems glad to help :)
0
 
skellstaffCommented:
I have the same problem in my database. The fields are also type text. After using nl2br, I get both newlines and <br /> entries in the database and this causes the html code to break when drawn from the database table. I found that the extra newlines, on new data entry, went away when using type char or varchar as the database field type.
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now