PHP 编码(猜猜UTF-8)
我正在编写一个系统,以便用户可以编辑他发布的内容。简化后,它是一个存储在数据库中的文本区域/输入字段和一个检索它的页面。问题是,我认为编码不好,因为字符串存储在数据库中,如“é”或其他东西(phpmyadmin 视图)。
插入页面:
- 我插入
mysql_real_escape_string($_POST['field ']);
输出页面:
- 来自数据库的对象。
htmlspecialchars($object->field);
但预期是: 输出页面:
- 来自数据库的对象。
htmlentities($object->field);
,对吧?
为什么MySql中的数据不能正确存储?
I'm programming a system so an user can edit what he posts. Simplified it's a textarea/input field which stores in a database and a page that retrieves it. The problem is, I think the encoding isn't okay, because strings are stored in the database like "é" or something (phpmyadmin view).
Insert page:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- I insert
mysql_real_escape_string($_POST['field']);
Output page:
- Object from database.
htmlspecialchars($object->field);
But expected is:
Output page:
- Object from database.
htmlentities($object->field);
, right?
Why isn't the data stored in MySql properly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您的数据库编码设置为utf-8,则还需要将传输编码设置为utf-8。为此,您必须
在插入表之前进行查询。
If your database encoding is set to utf-8, you need to set the tranfer encoding to utf-8, too. To do that you have to query
before inserting into the table.
数据库连接是UTF-8吗?请记住,这不是默认编码,您必须自己明确设置。
Is the database connection UTF-8 ? Bear in mind, that's not the default encoding, you have to explicitly set it yourself.
你必须告诉 htmlentities 你给它提供的是 UTF-8:
否则它会假设它获得了 ISO-8859-1。
You have to tell htmlentities that you feed it UTF-8:
Otherwise it assumes it gets ISO-8859-1.
本网站 http://developer.loftdigital.com/blog/php-utf-8 -cheatsheet 应该可以让您了解 PHP 和 MySQL 中 UTF-8 需要什么。
哦,不要忘记在连接到数据库后使用这些:
This site http://developer.loftdigital.com/blog/php-utf-8-cheatsheet should give you an idea of whats needed for UTF-8 in PHP and MySQL.
Oh and dont forget to use these after connecting to your DB: