Do I need to use HTML objects when storing data in a database?

I need to store special characters and characters in a mysql database. Therefore, either I can store it, like "ü", or convert it to html code, for example, "& uuml;"

I'm not sure it would be better.

I also have characters like "♥", "".

Please tell me which one is better? Also suggest if there is an alternative method.

Thanks.

+6
source share
3 answers

HTML objects were introduced many years ago to transfer symbolic information through a wire when the transport was not binary, and for the case when the user agent (browser) did not support the encoding of the transport layer or server encoding.

Since the HTML object contains only very simple characters ( & , ; , az and 0-9 ), and these characters have the same binary encoding in most character sets, this was very safe from those side effects.

However, when you store something in the database, you do not have these problems because you are usually under control and know how and how you can store text in the database.

For example, if you enable Unicode for text inside a database, you can store all characters, none of them are special. Please note that you need to know your database here, there are some technical details that you may encounter. For example, you don’t know the encoding of the encoding for connecting to the database, so you cannot tell your database exactly what text you want to save there. But in general, you just save the text and retrieve it later. Nothing special to solve.

There are actually disadvantages when you use HTML objects instead of a simple character:

  • HTML objects consume more space: ü much more than ü in LATIN-1, UTF-8, UTF-16 or UTF-32.
  • HTML objects need further processing. They must be created, and when they are read, they must be disassembled. Imagine that you need to find specific text in your database, or any other actions will require additional processing. This is just overhead.

Real entertainment begins when you mix both concepts. You come to a place that you really do not want to enter. So just don’t do it because you don’t need it.

+5
source

Leave your data raw in the database. Do not use HTML objects for this until you need them for HTML. You never know when you want to use your data elsewhere rather than on a web page.

+5
source

My suggestion will reflect other participants, do not convert special entities when saving them in your database.

Some reasons for not converting:

  • KISS Principle (my biggest reason not to do this)
  • most objects end up consuming more space than before conversion
  • to lose the ability to search for ü objects in a word will be [word]+ü+[/word] , and you will need to compare the strings of the equivalent html ü => [word]+ü+[/word] .
  • your output may change from HTML to say mobile API, etc., which makes conversion very unnecessary.
  • you need to convert the input and output (again, if your output changes from plain HTML to something else).
+1
source

Source: https://habr.com/ru/post/908547/


All Articles