Line break from MySQL longtext to HTML via PHP

I have multiple lines of text entered into a single MySQL longtext cell that I would like to display in HTML. However, I cannot figure out how to keep the line break when the string is displayed in HTML.

For example, let's say I have this text in the column "content":

|  content  |
| Line 1.   |
|           |
| Line 2.   |

I want this to display:

Line 1.

Line 2.

However, I am getting this result with my PHP loop:


I used the loop below to echo the text from my database. (I know I don't need a loop, but in the actual project I am displaying multiple rows of data).

$query = mysql_query("SELECT * FROM table ORDER BY timestamp DESC");
while($row = mysql_fetch_array($query)) {
    echo $row['title'];

In sum: How can I maintain line breaks when displaying a MySQL string in HTML? Is there any way to add <br> at the line breaks?


Try using php function nl2br (docs)

$content = "Line 1.\nLine 2.";
echo nl2br($content);
// output Line 1.<br>\nLine 2.

Need Your Help

Compilation Errors while building minioozie

apache maven hadoop oozie

I'm new to oozie and\ trying to install minioozie to test the workflows in oozie. I tried to follow the steps present in apache site

Regular expression for validating a mobile number

javascript jquery regex

I am using regular expressions to validate mobile numbers in Javascript.