mysql_real_escape_string 和 ’
我使用 mysql_real_escape_string 在将字符串插入到我的 mysql 数据库之前对其进行转义。
一切工作正常,除了字符 '
被丢失并被 mysql 变成 â‚â„„
。
我可以做什么来解决问题?我应该使用更好的函数来转义字符串吗?
我还担心其他角色可能会被错过并同样变成废话!
请帮忙!
谢谢 :)
I'm using mysql_real_escape_string to escape a string before inserting it into my mysql database.
Everything's working fine, except that the character ’
is getting missed and turned into ’
by mysql.
What can I do to get solve the problem? Should i be using a better function to escape the string?
I'm also worried that other charachters might be getting missed and being similarly turned into nonsense!
Please help!
Thanks :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
字符 ' 不会被遗漏,它只是 mysql 不使用的字符来封装字符串,并且不需要转义。
它变成那个奇怪的字符串的原因是因为它是一个多字节字符,而您将它放入一个单字节字段中。
The character ’ is not getting missed, it is simply a character that is not used by mysql for encasing strings, and does not need to be escaped.
The reason it is turning into that strange string is because it is a multi-byte character, and you are putting it into a single byte field.
您应该使用带有绑定变量的准备好的语句: http://php.net/ Manual/en/pdo.prepared-statements.php 这样你就不必担心转义任何东西。我链接到的文档中提到了这些优点。
You should be using prepared statements with bind variables instead: http://php.net/manual/en/pdo.prepared-statements.php This way you don't have to worry about escaping anything. The advantages are mentioned in the documentation I linked to.
mysql_real_escape_string() 只是转义一些字符(使用
\
's)使它们“安全”地推入您的查询。您的数据(样式引用)和列编码类型的编码似乎不匹配。 mysql_real_escape_string 永远不会解决此类问题。mysql_real_escape_string() simply escapes a handful of characters (using
\
's) to make them "safe" to shove into your query. You appear to have an encoding mismatch with your data (the styled quote) and your column encoding type. mysql_real_escape_string will never resolve that kind of issue.这是一个奇特的报价吗?如果是这样,由于字符编码差异,它在您的数据库中可能看起来像乱码。每个表都有一个关联的字符编码,并且连接有自己的编码。
尝试在查询之前执行“SET NAMES utf8”。这会将连接的编码设置为 UTF-8。当然,如果您尝试将 UTF-8 字符存储到 latin1 表中,您仍然不会得到您期望的结果。
Is that a fancy quote? If so, it probably looks like gibberish in your database due to character encoding differences. Each table has an associated character encoding, and the connection has its own encoding.
Try executing "SET NAMES utf8" before your query. That will set the encoding of the connection to UTF-8. Of course, if you are trying to store UTF-8 characters into, say a latin1 table, you will still not get the result you expect.
这是一个特殊字符,您需要使用 UTF 编码
将此行放在要在数据库中插入数据的页面顶部
希望它有效
That is a special character for this you need to use UTF Encoding
Place this line at the top of the page where you are inserting the data in database
Hope it works
如果您使用以下命令建立了 mysql 连接,它将起作用: mysql_query ("SET NAMES 'utf8'");
换句话说,如果没有设置SET NAMES 'utf8',则不需要utf8_encode。
It will work in case you had established the mysql connection with: mysql_query ("SET NAMES 'utf8'");
In other words, if SET NAMES 'utf8' is not set, utf8_encode is not needed.
更好的是使用 PDO 而不是标准 mysql。
http://www.php.net/manual/en/class.pdo.php
What would even be better is to use PDO instead of standard mysql.
http://www.php.net/manual/en/class.pdo.php
希望这会起作用。
Hope this will work.
输出:
INSERT INTO
test
.asd
(id
,name
,desc
)VALUES ('', 'asd\'lgh', 'knkk');1
Output:
INSERT INTO
test
.asd
(id
,name
,desc
)VALUES ('', 'asd\'lgh', 'knkk');1