在 MySQL INSERT INTO 文本中添加换行符
有人可以告诉我如何在 MySql 表中输入的文本中添加新行吗?
我尝试在使用 INSERT INTO
语句输入的行中使用 '\n'
,但 '\n'
按原样显示。
实际上我已经在 MS Access 中创建了一个包含一些数据的表。 MS Access 添加带有 '\n'
的新行。我正在将 MS Access 表数据转换为 MySql 。但是,当我转换时,'\n'
会被忽略,当我在 PHP 表单上的 MySql 表中显示它时,所有文本都显示在一行中。
谁能告诉我MySQL如何在文本中添加新行?等待回复,谢谢!!
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!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
如果您可以使用跨多行的 SQL 命令,那么 oedo 的 建议是最简单的:
我刚刚遇到过一种情况,最好将 SQL 语句全部放在一行上,所以我发现
CONCAT_WS()
和CHAR()
为我工作。If you're OK with a SQL command that spreads across multiple lines, then oedo's suggestion is the easiest:
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()
andCHAR()
worked for me.在实际的 SQL 查询中,只需添加换行符
in an actual SQL query, you just add a newline
作为记录,我想在现有数据中添加一些换行符,并且
\n
可以正常工作...示例数据:
我做到了:
但是,它也可以仅使用
\r
和只是\n
。For the record, I wanted to add some line breaks into existing data and I got
\n
to work ok...Sample data:
I did:
However, it also worked with just
\r
and just\n
.\n
在这里工作正常\n
just works fine here在大多数情况下,MySQL 可以很好地记录换行符,但问题是,您需要在实际字符串中使用
标签,以便浏览器显示换行符。既然您提到了 PHP,您可以使用nl2br()
函数将换行符(“\n
”)转换为 HTML
标签。只需像这样使用它:
输出(HTML):
这是手册的链接:http://php.net/manual/en/function.nl2br.php
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 thenl2br()
function to convert a linebreak character ("\n
") into HTML<br />
tag.Just use it like this:
Output (in HTML):
Here's a link to the manual: http://php.net/manual/en/function.nl2br.php
首先,如果您希望它显示在 PHP 表单上,媒介是 HTML,因此将使用
标记呈现新行。检查页面的源 HTML - 您可能会将新行呈现为换行符,在这种情况下,您的问题只是将文本翻译为输出到 Web 浏览器。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.在 SQL 或 MySQL 中,您可以使用
char
或chr
函数输入 ASCII 13 作为回车换行符,相当于\n
。但正如 @David M 所说,您很可能希望 HTML 显示此中断,而 br 就是有效的。In SQL or MySQL you can use the
char
orchr
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.在插入数据库之前,您必须将
\n
替换为
。$data = str_replace("\n", "
", $data);
在这种情况下,在数据库表中您将看到
而不是新行。例如
第一行
第二行
看起来像:
第一行
第二行
另一种查看数据的方式用新线。首先从数据库中读取数据。然后将
\n
替换为
例如:echo $data;
$data = str_replace("\n", "
", $data);
echo "
>" 。 $数据;
输出:
第一行第二行
第一行
第二行
您可以在这里找到有关函数 str_replace() 的详细信息: http://php.net/ Manual/en/function.str-replace.php
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
在 html 中使用
use
<pre>
tag instead of<p>
in html to show your\n
in database添加到给定 @DonKirby 的答案
是不必要的
CHAR()
函数不接受完整的utf8
值集。它仅接受ASCII
值。请参阅 - https://dev.mysql.com/ doc/refman/8.0/en/string-functions.html#function_char
因此,更合适的方法是使用
CHAR(10 USING ASCII)
代替CHAR(10 USING utf8 )
Adding to the answer given @DonKirby
is unnecessary
The
CHAR()
function doesn't accept the full set ofutf8
values. It accepts onlyASCII
values.See - https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_char
Thus more appropriate would be to use
CHAR(10 USING ASCII)
in place ofCHAR(10 USING utf8)
您可以简单地将所有
\n
替换为
标记,以便在显示页面时断行。You can simply replace all
\n
with<br/>
tag so that when page is displayed then it breaks line.