Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display html from a mysql database in php

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:

I have this in a mysql database table:

<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

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>
like image 616
Vince Avatar asked Mar 01 '13 08:03

Vince


1 Answers

Use 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>
like image 71
Siyuan Miao Avatar answered Oct 04 '22 18:10

Siyuan Miao