utf8 数据存储在数据库中时应该是什么样子?
我需要一些帮助来理解 utf8 数据存储在数据库中时的外观。
我使用 mysql 和 php,数据库设置为 utf8,列“p_name”上的排序规则设置为“utf8_unicode_ci”。
当我插入数据时,我通过这个函数传递它,
function convert_charset($in_str)
{
$cur_encoding = mb_detect_encoding($in_str) ;
if($cur_encoding == "UTF-8" && mb_check_encoding($in_str,"UTF-8"))
return $in_str;
else
return utf8_encode($in_str);
}
然后将其插入数据库。当我读出 php 中的数据并将其显示在浏览器中时,我在页面中使用 utf8,并且数据显示正确,但是我怀疑可能出了问题。
以法语单词 éditez
为例,它应该是这样的。然而,当我查看数据库时,相同的单词被存储为 éditez
。这是对的吗?这是 mysql 应该存储该单词的方式,还是我设置错误了?
这是我已经设置的:
- php.ini -->设置为utf8
- 数据库 -->设置为 utf8
- 表列 -->设置为 utf8_unicode_ci
- html 页面 -->设置为utf8。
I need a bit of help understanding how utf8 data is supposed to look when stored inside the database.
I'm using mysql and php, the database is set to utf8, the collation on the column "p_name" is set to "utf8_unicode_ci".
When I insert the data I pass it through this function
function convert_charset($in_str)
{
$cur_encoding = mb_detect_encoding($in_str) ;
if($cur_encoding == "UTF-8" && mb_check_encoding($in_str,"UTF-8"))
return $in_str;
else
return utf8_encode($in_str);
}
then insert it into the database. When I read out the data in php and display it in the browser i am using utf8 in the page, and the data displays right, however I'm suspecting something might be wrong.
Take the FRENCH word éditez
that's how it's supposed to look. However inside my database when I look at it the same word is being stored as éditez
. Is this right? Is this how mysql is supposed to store that word, or have I got something set wrong?
Here's what I have set already:
- php.ini --> set to utf8
- database --> set to utf8
- table column --> set to utf8_unicode_ci
- html pages --> set to utf8.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在数据库连接设置函数之后立即尝试此查询
mysql_query('SET NAMES "utf8"');
try this query right after your database connection setup functions
mysql_query('SET NAMES "utf8"');
您是否也尝试过将连接字符串设置为UTF8?
Have you tried setting your connectionstring to UTF8 as well?