Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a line break in MySQL INSERT INTO text

Could someone tell me how to add a new line in a text that I enter in a MySql table?

I tried using the '\n' in the line I entered with INSERT INTO statement but '\n' is shown as it is.

Actually I have created a table in MS Access with some data. MS Access adds new line with '\n'. I am converting MS Access table data into MySql . But when I convert, the '\n' is ignored and all the text is shown in one single line when I display it from MySql table on a PHP form.

Can anyone tell me how MySQL can add a new line in a text? Awaiting response, thanks!!

like image 528
Muhammed Umer Avatar asked May 25 '10 07:05

Muhammed Umer


People also ask

How do you insert a line break in text?

Press ALT+ENTER to insert the line break.

How do you store line breaks in database?

Just put the output text between <pre></pre> tags, that will preserve the line breaks.


2 Answers

If you're OK with a SQL command that spreads across multiple lines, then oedo's suggestion is the easiest:

INSERT INTO mytable (myfield) VALUES ('hi this is some text
and this is a linefeed.
and another');

I just had a situation where it was preferable to have the SQL statement all on one line, so I found that a combination of CONCAT_WS() and CHAR() worked for me.

INSERT INTO mytable (myfield) VALUES (CONCAT_WS(CHAR(10 using utf8), 'hi this is some text', 'and this is a linefeed.', 'and another'));
like image 134
Don Kirkby Avatar answered Oct 09 '22 15:10

Don Kirkby


in an actual SQL query, you just add a newline

INSERT INTO table (text) VALUES ('hi this is some text
and this is a linefeed.
and another');
like image 35
chris Avatar answered Oct 09 '22 15:10

chris