PHP MYSQL使用新行获取TEXT

时间:2011-07-10 04:23:26

标签: php mysql

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

这就是数据库的用途..

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

当我检索它们时,它们显示为..

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

它是一个TEXT字段..有没有办法让它们与新行一起出现?完全一样吗?

非常感谢!

3 个答案:

答案 0 :(得分:9)

使用此:

echo nl2br($result[msg]);

只要浏览器不将\r\n视为真正的新行,您就需要使用nl2br()将其“转换”为<br>

答案 1 :(得分:5)

也许nl2br你在找什么?

答案 2 :(得分:0)

您还可以使用:
REPLACE(`col_name`,"\n","<br />") as `col_name`
您必须在SELECT之后使用它。

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