How to display html from a mysql database in php

35,551

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>
Share:
35,551
Vince
Author by

Vince

Updated on August 04, 2022

Comments

  • Vince
    Vince over 1 year

    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>
    
  • Harsha
    Harsha almost 9 years
    This is working! why not accepted this answer. Really helpful.