在 MySQL INSERT INTO 文本中添加换行符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2902888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding a line break in MySQL INSERT INTO text
提问by Muhammed Umer
Could someone tell me how to add a new line in a text that I enter in a MySql table?
有人能告诉我如何在我在 MySql 表中输入的文本中添加新行吗?
I tried using the '\n'
in the line I entered with INSERT INTO
statement but '\n'
is shown as it is.
我尝试'\n'
在我用INSERT INTO
语句输入的行中使用 ,但'\n'
按原样显示。
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.
实际上,我在 MS Access 中创建了一个包含一些数据的表。MS Access 添加了新行'\n'
。我正在将 MS Access 表数据转换为 MySql 。但是当我转换时,'\n'
当我在 PHP 表单上从 MySql 表中显示它时,所有文本都被忽略,并且所有文本都显示在一行中。
Can anyone tell me how MySQL can add a new line in a text? Awaiting response, thanks!!
谁能告诉我 MySQL 如何在文本中添加新行?期待回复,谢谢!!
回答by Don Kirkby
If you're OK with a SQL command that spreads across multiple lines, then oedo'ssuggestion is the easiest:
如果您对跨多行的 SQL 命令没问题,那么oedo 的建议是最简单的:
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.
我只是有一种情况是最好有SQL语句都在同一行,所以我发现的组合CONCAT_WS()
,并CHAR()
为我工作。
INSERT INTO mytable (myfield) VALUES (CONCAT_WS(CHAR(10 using utf8), 'hi this is some text', 'and this is a linefeed.', 'and another'));
回答by chris
in an actual SQL query, you just add a newline
在实际的 SQL 查询中,您只需添加一个换行符
INSERT INTO table (text) VALUES ('hi this is some text
and this is a linefeed.
and another');
回答by Matt
For the record, I wanted to add some line breaks into existing data and I got \n
to work ok...
作为记录,我想在现有数据中添加一些换行符,然后我就\n
可以正常工作了...
Sample data:
样本数据:
Sentence. Sentence. Sentence
I did:
我做了:
UPDATE table SET field = REPLACE(field, '. ', '.\r\n')
However, it also worked with just \r
and just \n
.
但是,它也适用于 just\r
和 just \n
。
回答by xtds
INSERT INTO test VALUES('a line\nanother line');
\n
just works fine here
\n
在这里工作正常
回答by starleaf1
MySQL can record linebreaks just fine in most cases, but the problem is, you need <br />
tags in the actual string for your browser to show the breaks. Since you mentioned PHP, you can use the nl2br()
function to convert a linebreak character ("\n
") into HTML <br />
tag.
在大多数情况下,MySQL 可以很好地记录换行符,但问题是,您需要<br />
在浏览器的实际字符串中使用标签来显示换行符。既然您提到了 PHP,您可以使用该nl2br()
函数将换行符 (" \n
") 转换为 HTML<br />
标记。
Just use it like this:
只需像这样使用它:
<?php
echo nl2br("Hello, World!\n I hate you so much");
?>
Output (in HTML):
输出(在 HTML 中):
Hello, World!<br>I hate you so much
Here's a link to the manual: http://php.net/manual/en/function.nl2br.php
回答by nancy alajarmeh
INSERT INTO myTable VALUES("First line\r\nSecond line\r\nThird line");
回答by David M
First of all, if you want it displayed on a PHP form, the medium is HTML and so a new line will be rendered with the <br />
tag. Check the source HTML of the page - you may possibly have the new line rendered just as a line break, in which case your problem is simply one of translating the text for output to a web browser.
首先,如果您希望它显示在 PHP 表单上,则媒体是 HTML,因此将使用<br />
标记呈现一个新行。检查页面的源 HTML - 您可能将新行呈现为换行符,在这种情况下,您的问题只是将输出文本翻译到 Web 浏览器。
回答by RandyMorris
In SQL or MySQL you can use the char
or chr
functions to enter in an ASCII 13 for carriage return line feed, the \n
equivilent. But as @David M has stated, you are most likely looking to have the HTML show this break and a br is what will work.
在 SQL 或 MySQL 中,您可以使用char
或chr
函数输入 ASCII 13 作为回车换行符,\n
等效的。但正如@David M 所说,您最有可能希望让 HTML 显示此中断,而 br 是有效的。
回答by Airful
You have to replace
\n
with<br/>
before inset into database.$data = str_replace("\n", "<br/>", $data);
In this case in database table you will see
<br/>
instead of new line.e.g.
First Line
Second Line
will look like:
First Line<br/>Second Line
Another way to view data with new line. First read data from database. And then replace
\n
with<br/>
e.g. :echo $data;
$data = str_replace("\n", "<br/>", $data);
echo "<br/><br/>" . $data;
output:
First Line Second Line
First Line
Second Line
You will find details about function str_replace() here: http://php.net/manual/en/function.str-replace.php
必须更换
\n
与<br/>
之前插入到数据库中。$data = str_replace("\n", "<br/>", $data);
在这种情况下,您将在数据库表中看到
<br/>
而不是新行。例如
First Line
Second Line
看起来像:
First Line<br/>Second Line
另一种使用换行查看数据的方法。首先从数据库读取数据。然后
\n
用<br/>
例如替换:echo $data;
$data = str_replace("\n", "<br/>", $data);
echo "<br/><br/>" . $data;
输出:
First Line Second Line
First Line
Second Line
您可以在此处找到有关函数 str_replace() 的详细信息:http: //php.net/manual/en/function.str-replace.php
回答by uttam
You can simply replace all \n
with <br/>
tag so that when page is displayed then it breaks line.
您可以简单地\n
用<br/>
标签替换所有内容,以便在显示页面时它会换行。
UPDATE table SET field = REPLACE(field, '\n', '<br/>')