2017-04-10 42 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注入中保存你的腳本!

相關問題