Preserve line breaks in mysqli fetch_assoc( ) PHP

前端 未结 2 1780
-上瘾入骨i
-上瘾入骨i 2021-01-18 23:15

I am trying to get all the rows from my MySQL database using the following code:

$sql = \"SELECT * FROM myTable\";
$result = $conn->query($sql);
    while         


        
2条回答
  •  抹茶落季
    2021-01-18 23:49

    You need to know exactly what kind of text flow your json must send. Using php's nl2br function will replace newlines with a HTML tag named "br" used for displaying linebreaks when a browser render HTML. If the application that you feed with this json flow is expecting plain text and not HTML, using nl2br is not the right solution. In this case, I suggest to replace newlines with an escaped sequence as "\n" in your string before sending it as a parameter to the json_encode function, and to handle the decoding in your json flow consumer code adequately regarding what you want to do with it, html or anything else. In this case, this question may help.

提交回复
热议问题