0

I have a MySQL database with an InnoDB table containning utf8_general_ci varchar fields. When I fetch them through PHP (via PEAR::MDB2) and try to output them (via Smarty), I get ??? symbols. I would like to know how to fix that problem, which is most likely caused by PHP.

Good information to know:

  • It is a new version of the site I'm working on, the old version had the same problem even though it didn't use Smarty nor MDB2, so they are most likely not the cause. The old programmer used htmlentities() to remedy the problem, but I'm trying to avoid that.
  • The character encoding of all my files (template, source, etc.) is UTF-8 without BOM.
  • When I display a page, all accented characters (the ones in the templates, not the ones coming from MySQL) are shown correctly and the encoding in the browser is UTF-8. If I manually switch it over to ISO-8859-1, then the character from MySQL are outputed correctly, but no the others.

Basically, it seems that PHP or MySQL transforms the UTF-8 data contained within the database to ISO-8859-1 at some point during the query/fetch process, and that is what I want to fix.

I've done a lot of searching but haven't found any solution, and I'm hoping the problem lies in a setting somewhere. I'd like to avoid having to use htmlentities() or utf8_encode(), however that might be the only way to go until PHP6 shows up.

Thank you for your input on this!

4

2 回答 2

1

您需要执行一些查询来告诉它使用 UTF-8 进行连接(默认值确实是 Latin-1)。这是我使用的:

SET CHARACTER SET = "utf8";
SET character_set_database = "utf8";
SET character_set_connection = "utf8";
SET character_set_server = "utf8";

我知道其中一些看起来有点矫枉过正,但它们已经过测试并且似乎工作得很好......

于 2011-01-06T15:31:30.943 回答
0

我的猜测是数据在访问数据库时不是 utf-8 编码的。

于 2011-01-06T15:30:40.687 回答