3

I want to store HTML code in mysql database. If I want to store the following code into database

<html>
<body>
<p> this is a paragraph </p
</body>
</html>

they store as they are. But when I retrieve them and echo with php the tag get vanished. But I want to echo them as they are above. I also want to store and show not only HTML but other code (c,java,php) also. Anyone have any idea?

1
  • 3
    use htmlentities($whatever) Commented Oct 6, 2012 at 10:20

2 Answers 2

2

you can use htmlentities () php function to echo html codes

    $str = "
<html> 
 <body>
     <p> this is a paragraph </p
 </body>
</html>
";


echo htmlentities($str);

You can also use htmlspecialchars();

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

1 Comment

This is working. Do I do the same if I want to store other code like c or jave. Is there any problem to store c or java code in database directly? For storing HTML I am using mysql_real_escape_string(). Do I do the same for other code? Or is there any other procedure for storing other code? Thnx in advance.
1

You can use htmlentities($str) for that, another nice thing to use is <pre></pre> Putting those tags around the code will preserve newlines, tabs and spaces. In case you want to showcase it.

Comments

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.