插入数据库之前正在转义字符


Escaping characters before insert into database

我想在将一些字符插入数据库之前对其进行转换,例如

ß

我从帖子中获取数据,并在这个whay 上对其进行消毒

$ime =  (string) htmlentities(mysql_real_escape_string($data['txtIme']), ENT_COMPAT,'UTF-8', true);

出于某种原因,我仍然将Š,Š放入我的数据库

来源http://php.net/manual/en/function.htmlentities.php

所有具有HTML字符实体等价物的字符都被翻译成这些实体

如果你看http://www.ascii.cl/htmlcodes.htm

zh没有HTML字符实体等价物。Š确实如此。这就是为什么

<?php
     $string = 'In some reason I get still Ž, Š, Ć into my database';
     echo htmlentities($string, ENT_COMPAT,'UTF-8', true);
?>

产品:

$ php utf.php
In some reason I get still Ž, &Scaron;, Ć into my database

根据您想要将它们转换为什么,mysql_set_charset()可能是您需要使用的函数。使用UTF-8编码,您也可以存储大多数非标准字符。

但是,您应该小心使用mysql扩展,因为它已被弃用。您应该看看mysqliPDO