Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The best way to store &(Ampersand) in MySQL database

I'm building a category list and I'm unsure how to store the Ampersand symbol in MySQL database. Is there any problem/disadvantage if I use '&'. Are there any differences from using it in a html format '&amp'?

like image 649
inrob Avatar asked Jul 28 '12 20:07

inrob


3 Answers

Using '&' saves a few bytes. It is not a special character for MySQL. You can easily produce the & for output later with PHP's method htmlspecialchars(). When your field is meant to keep simple information, you should use plain text only, as this is in general more flexible since you can generate different kinds of markup etc. later. Exception: the markup is produced by a user whose layout decisions you want to save with the text (as in rich-text input). If you have tags etc. in your input, you may want to use & for consistency.

like image 82
Chris Avatar answered Sep 23 '22 12:09

Chris


You should store it as &amp; only if the field in the DB contains HTML (like <span class="bold">some text</span> &amp; more). In which case you should be very careful about XSS.

If the field contains some general data (like an username, title... etc) you should only escape it when you put it in your HTML (using htmlentities for example).

like image 40
Vatev Avatar answered Sep 23 '22 12:09

Vatev


Storing it as &amp; is an appropriate method. You can echo it or use it in statements as &amp;.

like image 43
ryno Avatar answered Sep 24 '22 12:09

ryno