Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to store html code in mysql? [duplicate]

Tags:

mysql

Possible Duplicate:
What structure type do I use for HTML content (MySQL)

What's the best way to store HTML code in MySQL?

If I have, let's say an entire page of source code, and I want to save it, what data type should I use for the column in MySQL?

Also, is it better to just save the code as a file, with a filename equal to the primary key, a unique id, created in MySQL (maybe to save space in the data file?)

like image 544
Francesco Avatar asked Feb 17 '12 03:02

Francesco


People also ask

Is it good to store html in database?

Storing HTML code is fine. But if it is not from trusted source, you need to check it and allow a secure subset of markup only. HTML Tidy library will help you with that. Also, you need to count with a future change in website design, so do not use too much markup, only basic tags.

How do I save an html database in SQL?

you can save the html codes just like text. You can use varchar(max) type column to save the html code in table. Display the code is depending the browser. But if you use nvarchar type that will cause problems in display.


2 Answers

I would recommend TEXT. I think blobs are more for multimedia etc. than text.

wikipedia blob

like image 102
Jeff Avatar answered Oct 02 '22 12:10

Jeff


Data type to store HTML in Database would be TEXT

But, you have to encode it using mysqli_real_escape_string
$dbh is the database connection handle you used to connect to your MySQL instance using mysqli_connect()

$html="Yor HTML"; $sql = "INSERT INTO tbl (html)  VALUES ('" . mysqli_real_escape_string($dbh,$html) . "')" 

or use Prepared Statements

like image 22
Naveen Kumar Avatar answered Oct 02 '22 13:10

Naveen Kumar