开发者

PHP MYSQL fetching a TEXT with new lines

开发者 https://www.devze.com 2023-03-18 08:36 出处:网络
<?php //after connecting etc.. $username = $_SESSION[\'username\']; $string = \"SELECT msg FROM $table WHERE username=\'$username\'\";
<?php

//after connecting etc..
        $username = $_SESSION['username'];
        $string = "SELECT msg FROM $table WHERE username='$username'";
    $result = mysql_fetch_array(mysql_query($string)) or die(mysql_error());
    echo $result[msg];
?>

this is how the database is..

I wanted to install the database base.
I wanted to install the database server.
I wanted to install the Ubuntu.

when i retrieve them they appear as..

I wanted 开发者_JS百科to install the database base.I wanted to install the database server.I wanted to install the Ubuntu.

Its a TEXT field.. is there a way to make them appear with the new lines? exactly the same?

Thanks a lot!


Use this:

echo nl2br($result[msg]);

As long as browsers doesn't treat \r\n as real new-lines - you need to "convert" them to <br>'s with nl2br()


Maybe nl2br is what are you looking for?


Also you can use:
REPLACE(`col_name`,"\n","<br />") as `col_name`
You must use this after SELECT.

Example code:
SELECT *, REPLACE(`name`,"\n","<br />") as `name` FROM xyz WHERE id="%s"

0

精彩评论

暂无评论...
验证码 换一张
取 消