Professional Web Applications Themes

preserving line breaks in mysql - PHP Development

I have a textarea in a form. When the value is added to a database, the linebreaks are lost. How can I preserve them ?...

  1. #1

    Default preserving line breaks in mysql

    I have a textarea in a form. When the value is added to a database, the
    linebreaks are lost. How can I preserve them ?
    meltedown Guest

  2. #2

    Default Re: preserving line breaks in mysql

    "meltedown" <org> skrev i en meddelelse
    news:oJzEe.562187$news.easynews.com... 

    Depends on what you mean by "lost". The "linebreaks" you see in a textarea
    are in fact newline characters (\n) and are saved as such in the database.
    And newline characters are usually ignored in HTML.
    But - you can convert them with nl2br():
    http://dk2.php.net/manual/en/function.nl2br.php
    Hope this helps.

    --
    bonfils
    http://kim.bonfils.com


    bonfils Guest

  3. #3

    Default Re: preserving line breaks in mysql

    bonfils wrote: 
    >
    >
    > Depends on what you mean by "lost". The "linebreaks" you see in a textarea
    > are in fact newline characters (\n) and are saved as such in the database.
    > And newline characters are usually ignored in HTML.
    > But - you can convert them with nl2br():
    > http://dk2.php.net/manual/en/function.nl2br.php
    > Hope this helps.[/ref]

    I'm talking about the "\n";
    When I put "test\n" into the database, and then call up that field from
    the database, I get "test". 
    meltedown Guest

  4. #4

    Default Re: preserving line breaks in mysql

    meltedown wrote: 
    >>
    >>
    >>
    >> Depends on what you mean by "lost". The "linebreaks" you see in a
    >> textarea are in fact newline characters (\n) and are saved as such in
    >> the database. And newline characters are usually ignored in HTML.
    >> But - you can convert them with nl2br():
    >> http://dk2.php.net/manual/en/function.nl2br.php
    >> Hope this helps.[/ref]
    >
    >
    > I'm talking about the "\n";
    > When I put "test\n" into the database, and then call up that field from
    > the database, I get "test".
    > [/ref]

    Post example code that produces this behaviour!

    Regards
    Stefan
    Stefan Guest

  5. #5

    Default Re: preserving line breaks in mysql

    Stefan Rybacki wrote: 
    >>
    >>
    >>
    >> I'm talking about the "\n";
    >> When I put "test\n" into the database, and then call up that field
    >> from the database, I get "test".
    >> [/ref]
    >
    > Post example code that produces this behaviour!
    >
    > Regards
    > Stefan[/ref]

    If I put this into the database:
    UPDATE client_vars SET name = 'test',value = 'test line 1
    test line 2';

    then:
    select * from client_vars where id=86
    results in : test line 1test line 2


    meltedown Guest

  6. #6

    Default Re: preserving line breaks in mysql

    > If I put this into the database: 


    Try using "mysql_escape_string" or "mysql_real_escape_string"
    functions. The correct way to insert special chars (like
    newline or apostrophe) into MySQL table fields is to use
    escape sequences like:

    UPDATE client_vars
    SET name = 'test',
    value = 'test line 1\ntest line 2'


    <?php
    $name = 'test';
    $value = 'test line 1
    test line 2';
    $query = 'UPDATE client_vars '
    . 'SET name=\'' . mysql_escape_string( $test ) . '\', '
    . ' value=\'' . mysql_escape_string( $value ) . '\'';
    ?>


    Hilarion

    PS.: If you have magic quotes turned on, then you'll have to turn
    it off (in this case you should manualy escape all strings
    entered by users) or use "stripslashes" before escaping the
    string.
    Hilarion Guest

  7. #7

    Default Re: preserving line breaks in mysql

    Hilarion wrote: 
    >
    >
    >
    > Try using "mysql_escape_string" or "mysql_real_escape_string"
    > functions. The correct way to insert special chars (like
    > newline or apostrophe) into MySQL table fields is to use
    > escape sequences like:
    >
    > UPDATE client_vars
    > SET name = 'test',
    > value = 'test line 1\ntest line 2'
    >
    >
    > <?php
    > $name = 'test';
    > $value = 'test line 1
    > test line 2';
    > $query = 'UPDATE client_vars '
    > . 'SET name=\'' . mysql_escape_string( $test ) . '\', '
    > . ' value=\'' . mysql_escape_string( $value ) . '\'';
    > ?>
    >
    >
    > Hilarion
    >
    > PS.: If you have magic quotes turned on, then you'll have to turn
    > it off (in this case you should manualy escape all strings
    > entered by users) or use "stripslashes" before escaping the
    > string.[/ref]
    I tried mysql_escape_string as you suggested, with partial success

    test line 1
    test line 2
    is changed to
    test line 1\r\ntest line 2 value2

    And it goes into the database like that.
    When I pull it out of the data base it is:
    test line 1
    test line 2

    Which is fine, but when I use that as the default for a textbox in a
    form, it shows up as:
    test line 1test line 2

    How do I get the new line to show up in the textbox ?
    If I use nl2br(),
    it shows up as
    test line 1<br />test line 2
    in the text box, instead of
    test line 1
    test line 2



    meltedown Guest

  8. Moderated Post

    Default Re: preserving line breaks in mysql

    Removed by Administrator
    Hilarion Guest
    Moderated Post

  9. Moderated Post

    Default Re: preserving line breaks in mysql

    Removed by Administrator
    meltedown Guest
    Moderated Post

Similar Threads

  1. Get rid of uselses line breaks?
    By edm203 in forum Macromedia Dynamic HTML
    Replies: 5
    Last Post: March 10th, 05:38 PM
  2. Respecting line breaks?
    By william in forum PHP Development
    Replies: 3
    Last Post: June 18th, 04:43 AM
  3. INSERTing Line Breaks into MySQL DB using ASP
    By Astra in forum ASP Database
    Replies: 1
    Last Post: June 11th, 11:33 AM
  4. Line Breaks (DB APP)
    By David R. Wheeler in forum Macromedia Dreamweaver
    Replies: 1
    Last Post: July 10th, 12:53 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139