正确的PHP方法在MySQL DB中存储特殊字符 [英] Correct PHP method to store special chars in MySQL DB

查看:350
本文介绍了正确的PHP方法在MySQL DB中存储特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用PHP,最好的方法是在MSQUL数据库中存储特殊字符(如下所示),以避免注入.

Using PHP, what is the best way to store special characters (like the following) in a MSQUL database, to avoid injections.

« " ' é à ù

这是我现在的操作方式:

This is how I do it now:

$book_text=$_POST['book_text'];
$book_text=htmlentities($book_text, "ENT_QUOTES");
$query=//DB query to insert the text

然后:

$query=//DB query to select the text
$fetch=//The fetch of $book_text
$book_text=html_entity_decode($book_text);

这样,我所有的文本都以HTML实体格式设置.但是我认为这占用了大量的数据库空间.那么,有没有更好的方法呢?

This way, all my text is formatted in HTML entities. But I think this takes up a lot of database space. So, is there a better way?

推荐答案

使用utf8编码存储这些值.

为避免注入,请使用mysql_real_escape_string()(或准备好的语句).

To avoid injections use mysql_real_escape_string() (or prepared statements).

要使用 XSS 进行保护,请使用htmlspecialchars.

To protect from XSS use htmlspecialchars.

这篇关于正确的PHP方法在MySQL DB中存储特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆