alexandreferris
alexandreferris

Reputation: 684

PHP - MySQL data is html code and php doesn't show correctly

I have some values in my database which a column named description contains HTML CODE ex. (<p>&nbsp;</p><div class="col-xs-8">..).

I get the database rows and try to show it in the page, all the columns displays normal (as it is plain text) BUT when I echo the variable that contains description value it doesn't turn the code into "page", it only shows the code.

Where is the problem? Shouldn't it consider as code as well and add it to the page?

CODE:

<div class="info">
   <div class="info-space" style="width: 800px;"></div>
        <?php
            echo $pro_desc;
        ?>
    </div>
</div>

Upvotes: 1

Views: 142

Answers (2)

Tuan Anh Hoang-Vu
Tuan Anh Hoang-Vu

Reputation: 1995

Your $pro_desc looks like this:

string '&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div class=&quot;col-xs-8&quot;...length(25000)

In order to display it correctly, you need to convert it to HTML format, for example &lt;p&gt; to <p> and so on. Try this:

echo "<p>".html_entity_decode($pro_desc)."</p>";

More info: http://php.net/manual/en/function.html-entity-decode.php

Upvotes: 1

Jorge Luis Jim&#233;nez
Jorge Luis Jim&#233;nez

Reputation: 1308

It's important that you try this code:

htmlspecialchars($_POST['description'] , ENT_QUOTES);

before you save your description in the database and when you need to fetch this field use :

htmlspecialchars_decode($data['description']);

You can read more here. http://php.net/htmlspecialchars

Upvotes: 0

Related Questions