php 如何在php中显示来自mysql数据库的html
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15153150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display html from a mysql database in php
提问by Vince
I'm stuck with trying to figure out how to display html tags inside of a mysql table. I've tried using addslashes, mysql_real_escape_string, as well as stripslashes to view the tags properly. Everytime I view the data through the browser, it shows the text of the html. Example:
我一直在试图弄清楚如何在 mysql 表中显示 html 标签。我试过使用addslashes、mysql_real_escape_string 以及stripslashes 来正确查看标签。每次我通过浏览器查看数据时,它都会显示 html 的文本。例子:
I have this in a mysql database table:
我在 mysql 数据库表中有这个:
<strong>Test</strong>
It should display as this when viewed on a webpage: Test
在网页上查看时它应该显示为: 测试
But instead, it displays <strong>Test</strong>
但相反,它显示 <strong>Test</strong>
My PHP code to view the content is:
我查看内容的PHP代码是:
<?php
require_once("inc.php"); //This includes the configuration for mysql database
?>
<html>
<head>
</head>
<body>
<p>
<?php
$conn = mysql_connect(HOST,USER,PASS) or die(mysql_error());
mysql_select_db(NAME) or die(mysql_error());
$sql = "SELECT * FROM events";
$query = mysql_query($sql);
while($row = mysql_fetch_assoc($query)){
echo(stripslashes($row['details'])); //The details is what contains the <strong>Test</strong>
}
mysql_close($conn) or die(mysql_error());
?>
</p>
</body>
</html>
回答by Siyuan Miao
Replace the following line
替换以下行
echo(stripslashes($row['details'])); //The details is what contains the <strong>Test</strong>
With
和
echo htmlspecialchars_decode(stripslashes($row['details'])); //The details is what contains the <strong>Test</strong>

