Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing HTML in MySQL

I'm storing HTML and text data in my database table in its raw form - however I am having a slight problem in getting it to output correctly. Here is some sample data stored in the table AS IS:

<p>Professional Freelance PHP & MySQL developer based in Manchester.
<br />Providing an unbeatable service at a competitive price.</p>

To output this data I do:

echo $row['details'];

And this outputs the data correctly, however when I do a W3C validator check it says:

character "&" is the first character of a delimiter but occurred as data

So I tried using htmlemtities and htmlspecialchars but this just causes the HMTL tags to output on the page.

What is the correct way of doing this?

like image 704
MAX POWER Avatar asked Dec 02 '22 22:12

MAX POWER


2 Answers

Use &amp; instead of &.

like image 160
newbie Avatar answered Dec 05 '22 13:12

newbie


What you want to do is use the php function htmlentities()...
It will convert your input into html entities, and then when it is outputted it will be interpreted as HTML and outputted as the result of that HTML...
For example:

$mything = "<b>BOLD & BOLD</b>";
//normally would throw an error if not converted...
//lets convert!!
$mynewthing = htmlentities($mything);

Now, just insert $mynewthing to your database!!

like image 31
pattyd Avatar answered Dec 05 '22 12:12

pattyd