如何在php中显示来自mysql数据库的html

时间:2022-09-25 16:39:32

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>

但相反,它显示测试

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>

1 个解决方案

#1


31  

Use htmlspecialchars_decode

使用htmlspecialchars_decode

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>

#1


31  

Use htmlspecialchars_decode

使用htmlspecialchars_decode

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>