duan19913 2012-09-30 21:17
浏览 30
已采纳

将编码设置为UTF8 - PHP [重复]

Possible Duplicate:
SET NAMES utf8 in MySQL?

I am writing PHP to connect to a MySQL database. I have seen some examples that sets the encoding to UTF8 mysql_query("SET NAMES utf8");

Why is this done? What are the advantages to doing this and what problems does it avoid?

  • 写回答

3条回答 默认 最新

  • doubi1797 2012-09-30 21:19
    关注

    This way the database will return data in UTF8 encoding (and expect such if you are inserting/updating). Useful if you are trying to query non-ascii characters from the server and displaying the content in general in UTF8 format, with UTF8 encoding headers. Otherwise you will see broken characters.

    Especially useful if the data in the database is in non-ascii and non-utf8 encoding/collation (like CP-1251 for cyrillic) or if you have multiple collations in same database. This way you ensure that you are getting/putting all the data in single universal encoding, that you both understand and like :)

    In general, it's a good practice. Most of the time you can't ensure you don't have data in non-ascii characters or there won't be such to insert. So it's just good practice to do it always, as UTF8 seems to be the widely accepted and loved data encoding these days.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误