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
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.