通过PHP从MySQL获取UTF-8字符串返回“????????”

时间:2022-10-24 20:43:51

In MySQL I have a string, which is displayed properly in phpmyadmin, with the collation set to utf8_general_ci .

在MySQL中我有一个字符串,它在phpmyadmin中正确显示,其排序规则设置为utf8_general_ci。

In the PHP file, I have set the header using plain HTML:

在PHP文件中,我使用纯HTML设置标头:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

The php files are hosted on Ubuntu, Apache 2.2, and the files are saved in UTF-8 without BOM (using Notepad++)

php文件托管在Ubuntu,Apache 2.2上,文件以UTF-8保存,没有BOM(使用Notepad ++)

I have set the connection to use utf8:

我已将连接设置为使用utf8:

@mysql_query("set names 'utf8'", $this->link);
$r = @mysql_query($query, $this->link);

it returns the string ( varchar 255 ) and using PHP echo , it is displayed to the browser.

它返回字符串(varchar 255)并使用PHP echo,它将显示给浏览器。

However, I am still getting ????? instead of the actual words.

但是,我还是得到?????而不是实际的话。

I can't seem to be able to searched for what I might have missed, your help is much appreciated.

我似乎无法搜索我可能错过的内容,非常感谢您的帮助。

===== Edit =====

=====编辑=====

Database, table and column can have three different collations, I have set them all to utf8_general_ci , and calling mysql_set_charset('utf8') immediately after the connection is made, have solved my problem.

数据库,表和列可以有三种不同的排序规则,我将它们全部设置为utf8_general_ci,并在建立连接后立即调用mysql_set_charset('utf8'),解决了我的问题。

I hope this will act as a useful checklist for future people bumping into the same problem. I for one may very well return in a few months to check if I have missed anything :)

我希望这将成为未来人们遇到同样问题的有用清单。我可以在几个月内回来检查我是否遗漏了任何东西:)

Thanks all for the great input.

非常感谢大家的投入。

4 个解决方案

#1


2  

Be sure your table is in utf8_general_ci. Database and tables can have a different charset (can happen when you change it by yourself).

确保你的表在utf8_general_ci中。数据库和表可以有不同的字符集(可以在您自己更改时发生)。

And be careful with some functions, like htmlentities, which have a default charset parameter set to ISO-8859-1 for PHP <5.4.0 (set to UTF-8 for PHP >5.4.0)

并注意一些函数,如htmlentities,其默认字符集参数设置为ISO <8859-1,PHP <5.4.0(对于PHP> 5.4.0设置为UTF-8)

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

#2


2  

I think the quotes around 'utf8' are wrong, but either way, you can just use:

我认为'utf8'的引用是错误的,但不管怎样,你可以使用:

mysql_set_charset('utf8');

And suppressing errors with the @ operator when you are looking for them, is not a good idea.

当你正在寻找它们时,用@运算符来抑制错误并不是一个好主意。

#3


-1  

Check something like this before the query

在查询之前检查这样的内容

mysql_query('SET character_set_results=utf8');
mysql_query('SET names=utf8');
mysql_query('SET character_set_client=utf8');
mysql_query('SET character_set_connection=utf8');
mysql_query('SET character_set_results=utf8');
mysql_query('SET collation_connection=utf8_general_ci');

#4


-1  

you can use this to encode the strings to UTF-8 so it will not get ???, but will change the non urf-8 characters like this /u003. so you can decode it later to present it on a text

您可以使用它来将字符串编码为UTF-8,因此它不会获得???,但会更改非urf-8字符,如/ u003。所以你可以稍后解码它以在文本上呈现它

while($var=mysql_fetch_assoc($r)){
           $e['columnI']=utf8_encode($e['columnI']);

#1


2  

Be sure your table is in utf8_general_ci. Database and tables can have a different charset (can happen when you change it by yourself).

确保你的表在utf8_general_ci中。数据库和表可以有不同的字符集(可以在您自己更改时发生)。

And be careful with some functions, like htmlentities, which have a default charset parameter set to ISO-8859-1 for PHP <5.4.0 (set to UTF-8 for PHP >5.4.0)

并注意一些函数,如htmlentities,其默认字符集参数设置为ISO <8859-1,PHP <5.4.0(对于PHP> 5.4.0设置为UTF-8)

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

#2


2  

I think the quotes around 'utf8' are wrong, but either way, you can just use:

我认为'utf8'的引用是错误的,但不管怎样,你可以使用:

mysql_set_charset('utf8');

And suppressing errors with the @ operator when you are looking for them, is not a good idea.

当你正在寻找它们时,用@运算符来抑制错误并不是一个好主意。

#3


-1  

Check something like this before the query

在查询之前检查这样的内容

mysql_query('SET character_set_results=utf8');
mysql_query('SET names=utf8');
mysql_query('SET character_set_client=utf8');
mysql_query('SET character_set_connection=utf8');
mysql_query('SET character_set_results=utf8');
mysql_query('SET collation_connection=utf8_general_ci');

#4


-1  

you can use this to encode the strings to UTF-8 so it will not get ???, but will change the non urf-8 characters like this /u003. so you can decode it later to present it on a text

您可以使用它来将字符串编码为UTF-8,因此它不会获得???,但会更改非urf-8字符,如/ u003。所以你可以稍后解码它以在文本上呈现它

while($var=mysql_fetch_assoc($r)){
           $e['columnI']=utf8_encode($e['columnI']);