简体   繁体   English

在MySQL数据库中存储&(Ampersand)的最佳方法

[英]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. 我正在构建一个类别列表,我不确定如何在AMP数据库中存储Ampersand符号。 Is there any problem/disadvantage if I use '&' . 如果我使用'&'是否有任何问题/劣势。 Are there any differences from using it in a html format '&amp' ? 在html格式'&amp'使用它有什么不同吗?

Using '&' saves a few bytes. 使用'&'可以节省几个字节。 It is not a special character for MySQL. 它不是MySQL的特殊字符 You can easily produce the & 您可以轻松制作& for output later with PHP's method htmlspecialchars() . 稍后使用PHP的方法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. 为了一致性。

You should store it as &amp; 你应该把它存储为&amp; only if the field in the DB contains HTML (like <span class="bold">some text</span> &amp; more ). 仅当数据库中的字段包含HTML(例如<span class="bold">some text</span> &amp; more )时。 In which case you should be very careful about XSS . 在这种情况下,您应该非常小心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). 如果该字段包含一些常规数据(如用户名,标题等),则只应在将其放入HTML中时将其转义(例如,使用htmlentities )。

Storing it as &amp; 将其存储为&amp; is an appropriate method. 是一种合适的方法。 You can echo it or use it in statements as &amp; 您可以回复它或在语句中使用它作为&amp; .

We store '&' into database fields all the time, it's fine to do so (at-least I've never heard an argument otherwise). 我们一直将'&'存储到数据库字段中,这样做很好(至少我从未听过其他的论点)。

If you're only ever using the string in a HTML page you could just store the HTML safe &amp; 如果您只是在HTML页面中使用该字符串,则只需存储HTML安全&amp; version I suppose. 我猜想的版本。 I would suggest that storing '&' and escaping it when you read it would be better though (in-case you need to use the string in a non-HTML context in the future). 我建议存储'&'并在读取它时转义它会更好(如果你将来需要在非HTML上下文中使用该字符串)。

Use &amp; 使用&amp; if you want to have a valid HTML or avoid problems, like cut&copy (browser shows it as cut© ). 如果您想拥有有效的HTML或避免出现问题,例如cut&copy (浏览器将其显示cut© )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM