69 users online (0 members and 69 guests)  


  Results 1 to 7 of 7

Related

  1. Replies: 2
  2. mysql query / function problem....    Forum: Databases
    Replies: 7
  3. Replies: 3
  4. Perl dbi with mysql problem    Forum: CGI Perl Forum
    Replies: 1
  5. I have a Problem with text line spacing    Forum: HTML Forum
    Replies: 1
  1. #1
    Mattias's Avatar
    New User

    Status
    Offline
    Join Date
    Mar 2006
    Posts
    2

    mysql & php new line problem

    I started learning php and mysql to create dynamic pages with a admin interface. I created it with the textarea tag and put the text into mysql and it looks like this when i see it in phpmyadmin

    This is a test
    this is line 2
    this is line 3

    but when outputting in php i get it all in the same row.
    i fetch the text from mysql and put it into a variable. somewhere along the line the new lines or linefeeds dissepear,
    does anyone have an awnser, i use php5 and mysql5

    thanx in advance/ Mattias

  2. #2
    vinyl-junkie's Avatar
    Moderator

    Status
    Offline
    Join Date
    Sep 2005
    Posts
    721

    Re: mysql & php new line problem

    Hi, Mattias, and welcome to the forum!

    The reason you're getting that is there are hidden new line characters "/n" (without the quotes) embedded in your text. To get rid of them and have your text display correctly in an HTML document, you need to use str_replace. For example:

    PHP Code:
    $line str_replace("\n","\n<br>","$line"); 
    Hope this helps.
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more

  3. #3
    Mattias's Avatar
    New User

    Status
    Offline
    Join Date
    Mar 2006
    Posts
    2

    Re: mysql & php new line problem

    Hi.
    Thanks!
    It worked great.

  4. #4
    tomas155's Avatar
    New User

    Status
    Offline
    Join Date
    Jul 2011
    Posts
    1

    Re: mysql & php new line problem

    And how will be if i want the output like
    PHP Code:
    echo "<a href='....' title='$line'>....</a>"

  5. #5
    jthayne's Avatar

    Status
    Offline
    Join Date
    Aug 2008
    Location
    Texas
    Posts
    508

    Re: mysql & php new line problem

    Quote Originally Posted by vinyl-junkie View Post
    PHP Code:
    $line str_replace("\n","\n<br>","$line"); 
    You can also use nl2br for this:
    PHP Code:
    echo nl2br($line); 
    Quote Originally Posted by tomas155 View Post
    And how will be if i want the output like
    PHP Code:
    echo "<a href='....' title='$line'>....</a>"
    Use the str_replace that vinyl_junkie mentioned before. Then you won't have to worry about the line breaks in the anchor title.
    PHP Code:
    $line str_replace("\n"""$line); 

  6. #6
    ellias's Avatar
    New User

    Status
    Offline
    Join Date
    Jul 2011
    Posts
    2

    Re: mysql & php new line problem

    Firstly I would like to suggest you to learn the PHP codes first. Then you do not face such kind of problem. some of the code of php is different then html. So I would like to suggest you to learn the codes from some good tutorial like w3schools.

  7. #7
    jthayne's Avatar

    Status
    Offline
    Join Date
    Aug 2008
    Location
    Texas
    Posts
    508

    Re: mysql & php new line problem

    Quote Originally Posted by ellias View Post
    some of the code of php is different then html.
    That statement has always bugged me. PHP and HTML are two completely different things. ALL of PHP code is different from HTML. HTML does only one thing: displays and formats data. It does not interpret the data nor do anything with servers. PHP on the other hand, gets the data from its source, determines what should happen with the data, and then creates the HTML code to display it.

    Quote Originally Posted by ellias View Post
    So I would like to suggest you to learn the codes from some good tutorial like w3schools.
    This is always good advice. I still use the tutorials as reference at times.



Tags for this Thread