-1

I read a text from my database which has multiple lines and and stored in a variable. when I output this variable on my webpage it is printed all in one line. How do output in different lines.

<?php
$num=$_GET['id'];
$result=mysql_query("SELECT * FROM messages WHERE id='$num'");
$row=mysql_fetch_array($result);
$msg=$row['message'];
?>
message:<br/><?php echo $msg;?>//Here I need to print message in multiple                               
                               //lines as entered in the database.
  • 2
    use [nl2br](http://php.net/manual/en/function.nl2br.php) – Iłya Bursov Apr 10 '17 at 18:16
  • 1
    ***Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).*** [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Apr 10 '17 at 18:16
  • 1
    [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)***. Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Apr 10 '17 at 18:16

1 Answers1

1

If you want to show the Exact Message as fetched from the database, Use <pre> tag!

Example:

<?php
$num=$_GET['id'];
$result=mysql_query("SELECT * FROM messages WHERE id='$num'");
$row=mysql_fetch_array($result);
$msg=$row['message'];
?>

message:<br/><?php echo "<pre>$msg</pre>";?>

Hope that helps!

Suggestion : Try not to use mysql_* functions as they are Deprecated now! Instead, Use PDO. It will save your script from SQL Injection too!

Adarsh Sojitra
  • 2,059
  • 1
  • 27
  • 50