1

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>
9
  • What is the result of this: var_dump($pro_desc);? Commented May 14, 2015 at 17:47
  • The database is holding a string, not markup. You have to store it in such a way so as to return the markup. Commented May 14, 2015 at 17:47
  • when you view the html source that your php page echoed, (ctrl+u) what does the html look like? Commented May 14, 2015 at 17:47
  • @RightClick it shows with a double quote in the beginning "<p>&nbsp;</p><div class="col-xs-8">... Commented May 14, 2015 at 17:49
  • @tuananh result of var_dump: string '&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div class=&quot;col-xs-8&quot;...length(25000) Commented May 14, 2015 at 17:50

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

7 Comments

So, it worked as I expected. (I have had read about it before). BUT is there any other option to make it work? As in, doing something in the database?
Why would you need another option @holpducki?
@JayBlanchard so I can have it configured in the database and not in my PHP/HTML files.
I will mark your answer as correct as it fixed my problem. But im still looking for anther option of fixing it. Thank you
there is no other option @holpducki
|
0

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

2 Comments

Do you have another option? As in configuring the database column?
I used that before and I implement this code [Html_Page] [nvarchar](max) NULL, in sqlserver. In Mysql you need to use a Text field. dev.mysql.com/doc/refman/5.7/en/blob.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.