Best way to store & (Ampersand) in a MySQL database

I am creating a list of categories and I am not sure how to store the Ampersand symbol in a MySQL database. Is there a problem / disadvantage if I use '&' . Are there any differences from using it in html '&' format?

+4
source share
5 answers

Using '&' saves a few bytes. This is not a special character for MySQL. You can easily create & for output later using the htmlspecialchars() PHP method. When your field is intended for storing simple information, you should use only plain text, since it is generally more flexible, since you can create different types of markup, etc. Later. Exception: markup is created by the user whose layout decisions you want to save with text (as in embedded text). If there are tags in your input, etc., you can use & for consistency.

+5
source

You must save it as &amp; , only if the field in the database contains HTML (for example, <span class="bold">some text</span> &amp; more ). In this case, you must be very careful with XSS .

If the field contains some common data (e.g. username, name ... etc.), you should avoid it when you put it in your HTML (using htmlentities ).

+3
source

Saving it as &amp; is a suitable method. You can repeat it or use in statements like &amp; .

+2
source

We store '&' in the database fields all the time, this is normal for this (at least I never heard an argument otherwise).

If you use only a line in an HTML page, you can just keep a safe version of HTML &amp; , I suppose. I would suggest that storing '&' and avoiding it when you read it would be better (if in the future you need to use a string in a context other than HTML).

+2
source

Use &amp; if you want to have valid HTML or avoid problems like cut© (the browser shows it as cutΒ© ).

+1
source

All Articles