Solved

Need some help with line breaks in MySQL database.

Posted on 2007-11-16
12
1,953 Views
Last Modified: 2013-12-13
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
Comment
Question by:graphxdiva
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 21

Accepted Solution

by:
nizsmo earned 400 total points
ID: 20297636
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
 
LVL 20

Assisted Solution

by:steelseth12
steelseth12 earned 100 total points
ID: 20297643
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
 
LVL 21

Expert Comment

by:nizsmo
ID: 20297644
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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 17

Expert Comment

by:Aleksandar Bradarić
ID: 20297648
>  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
 

Author Comment

by:graphxdiva
ID: 20297733
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
 
LVL 21

Expert Comment

by:nizsmo
ID: 20297817
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
 

Author Comment

by:graphxdiva
ID: 20297842
No if I create a new or edit an existing record.. in my text field enter

some

text

it displays

some
text

0
 
LVL 21

Expert Comment

by:nizsmo
ID: 20297964
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
 

Author Comment

by:graphxdiva
ID: 20298143
You mean text as your datatype in the table?  I was at medium text, switched to text and same thing.
0
 

Author Comment

by:graphxdiva
ID: 20300693
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
 
LVL 21

Expert Comment

by:nizsmo
ID: 20302451
weird thing! no problems glad to help :)
0
 

Expert Comment

by:skellstaff
ID: 25279413
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question