Php 从MySql检索数据后是否保留新行?

Php 从MySql检索数据后是否保留新行?,php,Php,这是我的SQL数据库中的数据: 但这是我在php中看到的: 如何保留新线,我通过这种方式获取数据: $q= "select * from commands where whether_executed=1 and whether_displayed=0 and from_who='".$user_id."' LIMIT 1" ; //mysql_set_charset("UTF8"); $ros=mysql_query($q,$link); if (mysql_num_rows($ro

这是我的SQL数据库中的数据:

但这是我在php中看到的:

如何保留新线,我通过这种方式获取数据:

  $q= "select * from commands where whether_executed=1 and whether_displayed=0 and from_who='".$user_id."' LIMIT 1" ;

//mysql_set_charset("UTF8");
$ros=mysql_query($q,$link);
if (mysql_num_rows($ros) > 0 ) {

while($row=mysql_fetch_array($ros))
{
    // update command set displayed to 1 so it doesnt appear each time
    $cmdId = $row['id'];
    $q2 = "update commands set whether_displayed=1 where from_who='".$user_id."' and id='".$cmdId."' LIMIT 1";
    mysql_query($q2);

    $response = $row['answer_type'].'!^#^^#^!'.$row['answer'];

    echo json_encode($response);

}
} else {
echo "NULL";
}

此行将换行符更改为“\n”:


这是因为当内容类型为html时,您将响应发送到不尊重新行字符(就显示而言)的浏览器

将响应内容类型设置为“文本/普通”,新的行将显示在浏览器中


或者,如果您出于任何原因希望页面显示为html,您可以在输出之前将出现的
\n
替换为

我注意到在
.jpg
之后需要换行符,因此此方法可能有效

str_replace ('.jpg' , '.jpg\n' , $ros);

用换行符替换所有
.jpg

使用echo$response而不是json\u encode。或者,如果必须使用json编码,则接受换行符替换为\n,因为换行符在json字符串中无效(尽管json解析器将\n解码为换行符)。
str_replace ('.jpg' , '.jpg\n' , $ros);