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 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作