Reputation: 1075
I need to store special characters and symbols into mysql database. So either I can store it as it is like 'ü' or convert it to html code such as 'ü'
I am not sure which would be better.
Also I am having symbols like '♥', '„' .
Please suggest which one is better? Also suggest if there is any alternative method.
Thanks.
Upvotes: 6
Views: 5744
Reputation: 20475
My suggestion would mirror the other contributors, don't convert the special entities when saving them to your database.
Some reasons against conversion:
ü
in a word, would be [word]+ü+[/word]
, and you would have to do a string comparison of the html equivalent of ü
=> [word]+ü+[/word]
.Upvotes: 2
Reputation: 197658
HTML entities have been introduced years ago to transport character information over the wire when transportation was not binary safe and for the case that the user-agent (browser) did not support the charset encoding of the transport-layer or server.
As a HTML entity contains only very basic characters (&
, ;
, a-z
and 0-9
) and those characters have the same binary encoding in most character sets, this is and was very safe from those side-effects.
However when you store something in the database, you don't have these issues because you're normally in control and you know what and how you can store text into the database.
For example, if you allow Unicode for text inside the database, you can store all characters, none is actually special. Note that you need to know your database here, there are some technical details you can run into. Like you don't know the charset encoding for your database connection so you can't exactly tell your database which text you want to store in there. But generally, you just store the text and retrieve it later. Nothing special to deal with.
In fact there are downsides when you use HTML entities instead of the plain character:
ü
is much larger than ü
in LATIN-1, UTF-8, UTF-16 or UTF-32.The real fun starts when you mix both concepts. You come to a place you really don't want to go into. So just don't do it because you ain't gonna need it.
Upvotes: 5
Reputation: 163281
Leave your data raw in the database. Don't use HTML entities for these until you need them for HTML. You never know when you may want to use your data elsewhere, not on a web page.
Upvotes: 5