在 MySQL INSERT INTO 文本中添加换行符

发布于 2024-09-03 08:09:30 字数 351 浏览 2 评论 0原文

有人可以告诉我如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

离旧人 2024-09-10 08:09:30

如果您可以使用跨多行的 SQL 命令,那么 oedo 的 建议是最简单的:

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

我刚刚遇到过一种情况,最好将 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'));

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'));
划一舟意中人 2024-09-10 08:09:30

在实际的 SQL 查询中,只需添加换行符

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

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');
情深已缘浅 2024-09-10 08:09:30

作为记录,我想在现有数据中添加一些换行符,并且 \n 可以正常工作...

示例数据:

Sentence. Sentence. Sentence

我做到了:

UPDATE table SET field = REPLACE(field, '. ', '.\r\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:

Sentence. Sentence. Sentence

I did:

UPDATE table SET field = REPLACE(field, '. ', '.\r\n')

However, it also worked with just \r and just \n.

ヅ她的身影、若隐若现 2024-09-10 08:09:30
INSERT INTO test VALUES('a line\nanother line');

\n 在这里工作正常

INSERT INTO test VALUES('a line\nanother line');

\n just works fine here

﹎☆浅夏丿初晴 2024-09-10 08:09:30

在大多数情况下,MySQL 可以很好地记录换行符,但问题是,您需要在实际字符串中使用
标签,以便浏览器显示换行符。既然您提到了 PHP,您可以使用 nl2br() 函数将换行符(“\n”)转换为 HTML
标签。

只需像这样使用它:

<?php
echo nl2br("Hello, World!\n I hate you so much");
?>

输出(HTML):

Hello, World!<br>I hate you so much

这是手册的链接: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 the nl2br() function to convert a linebreak character ("\n") into HTML <br /> tag.

Just use it like this:

<?php
echo nl2br("Hello, World!\n I hate you so much");
?>

Output (in HTML):

Hello, World!<br>I hate you so much

Here's a link to the manual: http://php.net/manual/en/function.nl2br.php

花开柳相依 2024-09-10 08:09:30
INSERT INTO myTable VALUES("First line\r\nSecond line\r\nThird line");
INSERT INTO myTable VALUES("First line\r\nSecond line\r\nThird line");
苏别ゝ 2024-09-10 08:09:30

首先,如果您希望它显示在 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.

匿名。 2024-09-10 08:09:30

在 SQL 或 MySQL 中,您可以使用 charchr 函数输入 ASCII 13 作为回车换行符,相当于 \n。但正如 @David M 所说,您很可能希望 HTML 显示此中断,而 br 就是有效的。

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.

野味少女 2024-09-10 08:09:30
  1. 在插入数据库之前,您必须将 \n 替换为

    $data = str_replace("\n", "
    ", $data);

    在这种情况下,在数据库表中您将看到
    而不是新行。

    例如

    第一行
    第二行

    看起来像:

    第一行
    第二行

  2. 另一种查看数据的方式用新线。首先从数据库中读取数据。然后将 \n 替换为
    例如:

    echo $data;

    $data = str_replace("\n", "
    ", $data);


    echo "

    >" 。 $数据;

    输出:

    第一行第二行

    第一行
    第二行

    您可以在这里找到有关函数 str_replace() 的详细信息: http://php.net/ Manual/en/function.str-replace.php

  1. 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

  2. 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

谜兔 2024-09-10 08:09:30

在 html 中使用

 标签代替 

来显示数据库中的 \n

use <pre> tag instead of <p> in html to show your \n in database

第几種人 2024-09-10 08:09:30

添加到给定 @DonKirby 的答案

INSERT INTO mytable (myfield) VALUES (CONCAT_WS(CHAR(10 using utf8), 'hi this is some text', 'and this is a linefeed.', 'and another'));

是不必要的

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

INSERT INTO mytable (myfield) VALUES (CONCAT_WS(CHAR(10 using utf8), 'hi this is some text', 'and this is a linefeed.', 'and another'));

is unnecessary

The CHAR() function doesn't accept the full set of utf8 values. It accepts only ASCII 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 of CHAR(10 USING utf8)

Bonjour°[大白 2024-09-10 08:09:30

您可以简单地将所有 \n 替换为
标记,以便在显示页面时断行。

UPDATE table SET field = REPLACE(field, '\n', '<br/>')

You can simply replace all \n with <br/> tag so that when page is displayed then it breaks line.

UPDATE table SET field = REPLACE(field, '\n', '<br/>')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文