programing tip

MySQL INSERT INTO 텍스트에 줄 바꿈 추가

itbloger 2020. 11. 14. 09:59
반응형

MySQL INSERT INTO 텍스트에 줄 바꿈 추가


누군가 내가 MySql 테이블에 입력하는 텍스트에 새 줄을 추가하는 방법을 알려줄 수 있습니까?

나는 문장으로 '\n'입력 한 줄에서 사용하려고 시도 INSERT INTO했지만 '\n'그대로 표시됩니다.

실제로 MS Access에서 일부 데이터로 테이블을 만들었습니다. MS Access는 '\n'. MS Access 테이블 데이터를 MySql로 변환하고 있습니다. 그러나 변환 할 때는 '\n'무시되고 PHP 양식의 MySql 테이블에서 표시 할 때 모든 텍스트가 한 줄에 표시됩니다.

누구든지 MySQL이 텍스트에 새 줄을 추가하는 방법을 말해 줄 수 있습니까? 응답을 기다리고 있습니다, 감사합니다 !!


여러 줄에 걸쳐있는 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'));

실제 SQL 쿼리에서는 개행 문자 만 추가하면됩니다.

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

기록을 위해 기존 데이터에 줄 바꿈을 추가하고 싶었고 \n정상적으로 작업했습니다.

샘플 데이터 :

Sentence. Sentence. Sentence

나는했다 :

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

그러나 just \r및 just 와도 함께 작동했습니다 \n.


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

\n 여기서 잘 작동합니다.


MySQL은 대부분의 경우 <br />줄 바꿈을 잘 기록 할 수 있지만 문제는 브라우저에서 줄 바꿈을 표시하려면 실제 문자열에 태그가 필요하다는 것 입니다. PHP를 언급 했으므로이 nl2br()함수를 사용하여 줄 바꿈 문자 ( " \n")를 HTML <br />태그 변환 할 수 있습니다 .

다음과 같이 사용하십시오.

<?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


INSERT INTO myTable VALUES("First line\r\nSecond line\r\nThird line");

우선, PHP 양식에 표시하려면 매체가 HTML이므로 <br />태그 와 함께 새 줄이 렌더링됩니다 . 페이지의 소스 HTML을 확인하십시오. 줄 바꿈처럼 새 줄이 렌더링 될 수 있습니다.이 경우 문제는 단순히 웹 브라우저에 출력 할 텍스트를 번역하는 것입니다.


  1. 당신은 교체해야 \n<br/>데이터베이스에 전에 삽입.

    $data = str_replace("\n", "<br/>", $data);

    이 경우 데이터베이스 테이블에서 <br/>줄 바꿈 대신 볼 수 있습니다 .

    예 :

    First Line
    Second Line

    다음과 같이 보일 것입니다.

    First Line<br/>Second Line

  2. 새 줄로 데이터를 보는 또 다른 방법입니다. 먼저 데이터베이스에서 데이터를 읽습니다. 그런 다음 \n<br/>들면 다음 과 같이 바꿉니다.

    echo $data;
    $data = str_replace("\n", "<br/>", $data);
    echo "<br/><br/>" . $data;

    산출:

    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


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.


It sounds to me like you're confusing the contents of the table some particular tool's rendering of those contents.

I presume you're using the command line (shell) client, the mysql prompt both to enter and to display the text in this column. I'm also guessing that you're \n is being stored as a newline and that it's only the way it's being presented that's causing your confusion.

Try using a GUI tool for accessing your MySQL data.


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/>')

참고URL : https://stackoverflow.com/questions/2902888/adding-a-line-break-in-mysql-insert-into-text

반응형