简体   繁体   English

PHP和MySQL的utf-8编码的字符串问题?

[英]string issue of utf-8 encoding with PHP and MySQL?

This is what I tried so far,by modifying php.ini: default_charset = "utf-8" 到目前为止,这是我通过修改php.ini进行的尝试:default_charset =“ utf-8”

This is how MySQL is configured: 这是配置MySQL的方式:

mysql> show variables like '%char%';
+--------------------------+-----------------------------------------------+
| Variable_name            | Value                                         |
+--------------------------+-----------------------------------------------+
| character_set_client     | utf8                                          |
| character_set_connection | utf8                                          |
| character_set_database   | utf8                                          |
| character_set_filesystem | binary                                        |
| character_set_results    | utf8                                          |
| character_set_server     | utf8                                          |
| character_set_system     | utf8                                          |
| character_sets_dir       | f:\wamp\bin\mysql\mysql5.0.45\share\charsets\ |
+--------------------------+-----------------------------------------------+
8 rows in set (0.00 sec)

But it doesn't work. 但这是行不通的。

I've dumped the data,it is in utf-8 format before and after processing by PHP. 我已经转储了数据,在PHP处理之前和之后它都是utf-8格式。

But after inserting into MySQL by PHP, 但是在通过PHP插入MySQL之后,

it becomes a big mess(through phpmyadmin), 它变得一团糟(通过phpmyadmin),

The data became something like this after inserting into database: 插入数据库后,数据变为如下所示:

江è‹çœ 江阴市 山观镇 西è‹' æ–°æ' 江è‹çœæ±Ÿé〜´å¸,山观镇西è‹'æ–°æ'

what's worse,the data become normal again after reading it from MySQL and showing on the page! 更糟糕的是,从MySQL读取并显示在页面上之后,数据又恢复了正常!

Can someone point out what's the real problem here? 有人可以指出这里的真正问题是什么?

The code are all things like: 代码都是这样的:

$dml = "insert into profiles(accountId,name,thumbnail,sex,homeAddr,livingAddr,peoples,married,politicalStatus,qq,mobilePhone,telephone,homePage) 
            value($accountId,'{$_POST['name']}',{$_POST['thumbnail']},{$_POST['sex']},'{$_POST['homeAddr']}','{$_POST['livingAddr']}','{$_POST['peoples']}',{$_POST['married']},'{$_POST['politicalStatus']}',{$_POST['qq']},{$_POST['mobilePhone']},{$_POST['telephone']},{$_POST['homePage']})";
mysql_query($dml,$con);

Look at the connection parameters in the php config within phpMyAdmin. 查看phpMyAdmin中php配置中的连接参数。 When you connect to a database, you can specify an encoding of the connection (as well as all these other places). 连接数据库时,可以指定连接的编码(以及所有其他位置)。 That could be out of sync causing you problems. 这可能不同步,从而导致您遇到问题。

Please be more specific about your problem. 请更具体地说明您的问题。 Post the code that causes the problem, and say something about how it "doesn't work". 张贴导致问题的代码,并说说它“不起作用”的方式。 Without code, we can't solve your problem. 没有代码,我们将无法解决您的问题。

That said, make sure your phpMyAdmin charsets are set appropriately. 也就是说,请确保正确设置了phpMyAdmin字符集。 I think you're trying to say that it looks wrong in phpMyAdmin, in which case that is likely to be where your problem is. 认为您试图说的是phpMyAdmin中的错误,在这种情况下,很可能就是您的问题所在。

Edit: It's unclear what "what's worse,the data become normal again after reading it from MySQL and showing on the page!" 编辑:目前尚不清楚“更糟糕的是,从MySQL读取数据并显示在页面上之后,数据再次恢复正常!” means. 手段。

Your comment on the other answer doesn't really prove anything. 您对其他答案的评论并不能真正证明任何事情。 Just because it looks right in phpMyAdmin doesn't mean it agrees with what's in the database. 仅仅因为它在phpMyAdmin中看起来正确并不意味着它与数据库中的内容一致。 Your php character set needs to match your database character set needs to match your phpMyAdmin character set. 您的php字符集需要匹配您的数据库字符集,需要匹配您的phpMyAdmin字符集。 If ANY of them are wrong, you will get EXACTLY this problem. 如果它们中的任何一个是错误的,您都将确切地遇到此问题。 One of them does not match! 其中之一不匹配!

make sure you HTTP content type is utf-8. 确保您的HTTP内容类型为utf-8。 that's a tricky one to nail down. 这是一个棘手的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM