2017-04-10 41 views
-1

我从我的数据库中读取了一个文本,它有多行并存储在一个变量中。 当我在我的网页上输出这个变量时,它全部打印在一行中。 如何在不同的行输出。不输出换行符

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

使用[nl2br](http://php.net/manual/en/function.nl2br.php) –

+1

***请[停止使用'mysql_ *'函数](http://stackoverflow.com/questions /12859942/why-shouldnt-i-use-mysql-functions-in-php).*** [这些扩展程序](http://php.net/manual/en/migration70.removed-exts-sapis.php)已在PHP 7中删除。了解[准备](http://en.wikipedia.org/wiki/Prepared_statement)[PDO]的声明(http://php.net/manual/en/pdo.prepared-statements .php)和[MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)并考虑使用PDO,[这真的很简单](http://jayblanchard.net/ demystifying_php_pdo.html)。 –

+1

[Little Bobby](http://bobby-tables.com/)说*** [你的脚本存在SQL注入攻击风险。](http://stackoverflow.com/questions/60174/how-can- I-防止-SQL注入式-PHP)***。即使[转义字符串](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string)是不安全的! –

回答

1

如果要显示从数据库中提取的确切消息,请使用<pre>标记!

例子:

<?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>";?> 

希望帮助!

意见:尽量不要使用mysql_ *功能,因为它们现在已被弃用!相反,使用PDO。它也会从SQL注入中保存你的脚本!

相关问题