dongsuo0517 2013-04-22 15:37
浏览 118
已采纳

UTF-8给了我问号

I have this simple PHP-script, which searches a mySQL database and outputs the result to the user. I used to use ISO-8859-1 as my charset, but was advised to use UTF-8. But I have trouble going from my former charset to the new one.

To clarify some things, I have:

  • Created a database and table encoded in UTF-8 with collation utf8_unicode_ci.
  • Encoded my PHP-file in UTF-8.
  • Set meta charset to UTF-8.
  • Set all text mime-types to UTF-8 through create-mime.assign.pl in Lighty (Lighttpd).

Now, the problem arises when I retrieve data from the database with characters like ö, ü etc. If I just do echo "ö"; without retrieving it from the database, it works fine. I guess there must be something wrong with the database then?

I've tried the following, and they've solved my problem:

  • Set meta charset to ISO-8859-1 (which, for some strange reason works, but breaks the echo'd "ö").
  • Set a utf8_decode() function around the output.
  • After mysql_select_db() declared the following mysql_set_charset('utf8');.

I know that I've found multiple solutions, but I just don't know why it wont work without them? And is it bad practice to use utf8_decode() on output, or the mysql_set_charset() function?

  • 写回答

1条回答 默认 最新

  • dongwende1984 2013-04-22 15:43
    关注

    MySQL is funny with UTF8. You need to ensure the server is running in UTF and that the connection is as well

    If you can modify the my.cnf file on your server you can add these to the [mysqld] section and restart it

    character-set-server = utf8
    skip-character-set-client-handshake
    

    You could alternatively (or as well) use

    query("SET NAMES utf8");   
    

    before sending/retrieving data to ensure the database expects UTF8 data to be passed

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退