【问题标题】:How to display html from a mysql database in php如何在php中显示来自mysql数据库的html
【发布时间】:2013-02-15 16:24:33
【问题描述】:

我一直在试图弄清楚如何在 mysql 表中显示 html 标记。我尝试使用addslashes、mysql_real_escape_string 以及stripslashes 来正确查看标签。每次我通过浏览器查看数据时,它都会显示 html 的文本。示例:

我在 mysql 数据库表中有这个:

<strong>Test</strong>

在网页上查看时应显示如下: 测试

但相反,它显示&lt;strong&gt;Test&lt;/strong&gt;

我查看内容的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>

【问题讨论】:

  • 你用什么代码来显示数据?另外,根据您的问题,您可能应该将 PHP 添加为标签。
  • 您使用的框架很可能会自动输出转义以防止 xss 攻击。在我知道的所有框架中,有一种简单的方法可以在不转义的情况下输出字符串。您应该将框架的名称添加到问题中。
  • 我正在使用 PHP 来显示数据。

标签: php html mysql database tags


【解决方案1】:

使用htmlspecialchars_decode

替换下面一行

  echo(stripslashes($row['details'])); //The details is what contains the <strong>Test</strong>

  echo htmlspecialchars_decode(stripslashes($row['details'])); //The details is what contains the <strong>Test</strong>

【讨论】:

  • 这行得通!为什么不接受这个答案。真的很有帮助。
猜你喜欢
  • 2017-07-15
  • 1970-01-01
  • 1970-01-01
  • 2020-05-19
  • 1970-01-01
  • 1970-01-01
  • 2018-11-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多