dongtiao6362 2012-10-27 15:31
浏览 77
已采纳

Mysql php字符集设置

At first we was using the default character set by the mysql that was latin1_swedish_ci. Then we had problem with other languages like russian etc. So we have now converted the tables into collation and the field into utf8_unicode_ci. We need some confirmation here should we stick with utf8_unicode_ci for the support all the possible languages or go for something else like utf8_bin?

Next issue for php files where we are using mysqli we have set this.

if (!mysqli_set_charset($link, "utf8")) {
    echo("Error loading character set utf8: ". mysqli_error($link));
  } 
  else {
    echo("Current character set: ". mysqli_character_set_name($link));
  }

Then for places where we use mysql only we did as below

mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET COLLATION_CONNECTION = 'utf8_unicode_ci'"); 

The problem now we find this is like quite tedious and we are scared we might be miss. Is it possible to set this character setting in like a config file like we have one for our db connection?

  • 写回答

1条回答 默认 最新

  • doudou130216 2012-10-27 15:38
    关注

    Don't mix mysql_* with mysqli_* functions. You need to stay consistent! You use mysqli_ first and then you use mysql_. That won't work!

    This is how I do it:

    mysqli_set_charset($Handle, 'utf8'); // <- add this too
    mysqli_query($Handle, "SET NAMES 'utf8';");
    mysqli_query($Handle, "SET CHARACTER SET 'utf8';");
    mysqli_query($Handle, "SET COLLATION_CONNECTION = 'utf8_unicode_ci';");
    // might be a bit redundant but it's safe :) ... I think :)
    

    Then make sure you provide proper UTF8 to it.

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

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看