Preserve line breaks in mysqli fetch_assoc( ) PHP

前端 未结 2 1781
-上瘾入骨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:48

    Using array_map you can call the function, so with the value of each $row you can get the values.

    $sql = "SELECT * FROM myTable";
     $result = $conn->query($sql);
         while($row = $result->fetch_assoc()) {
             $output[]=array_map("nl2br",$row);
         }
    echo json_encode($output);
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题