Adding a line break in MySQL INSERT INTO text

后端 未结 10 2043
太阳男子
太阳男子 2020-12-07 22:07

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 I

相关标签:
10条回答
  • 2020-12-07 22:26

    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

    0 讨论(0)
  • 2020-12-07 22:33

    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.

    0 讨论(0)
  • 2020-12-07 22:35

    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/>')
    
    0 讨论(0)
  • 2020-12-07 22:39
    INSERT INTO myTable VALUES("First line\r\nSecond line\r\nThird line");
    
    0 讨论(0)
提交回复
热议问题