dpjhq00684 2013-06-13 12:28
浏览 88
已采纳

使用mysql_real_escape_string编码错误,返回空字符串

The scenario: An html-document that's used for editing product details is encoded as ISO-8859-1, and sends POST data to a PHP-file (also encoded ISO-8859-1). That PHP-file in turn has mysql_real_escape_string -functions cleaning the inputs. The database/mysql server charset is UTF-8. The problem is that when a POST string has scandinavian letters (ä,ö,å) in it, the mysql_real_escape_string returns an empty string.

I tried to override it with utf8_encode/decode, but it won't work.

Now, if I change the html&php-files to use UTF-8, saving works fine (as expected), but then all previously saved product data looks corrupt, and there are thousands of product rows.

What would be the easiest solution to fix it? Change the db charset? Run a encoding function to convert the entries in the db?

  • 写回答

1条回答 默认 最新

  • donglipi4495 2013-06-13 13:09
    关注

    mysql_real_escape_string considers the current connection character set. Your ISO-8859-1 scripts should work if you set your connection explicitly to UTF-8 before calling mysql_real_escape_string (query "SET NAMES 'utf8'" before using mysql_real_escape_string)

    But for sure I would always recommend to use unicode (and only unicode) wherever it is possible;)

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

报告相同问题?

悬赏问题

  • ¥15 TMC2209串口模式下读取不到寄存器的值串口助手蓝色字体是发过去的消息,绿色字体是收到的消息,第二行发送读取寄存器的指令但是没有读取到寄存器的值串口助手如下图:接线如下图,如何解决?
  • ¥30 itest不允许查看成绩怎么办
  • ¥15 高通安卓11提取完整线刷包软件,或者优博讯dt50顺丰刷机包
  • ¥20 C,有个译码器,换了信道就跑不出原来数据
  • ¥15 MIMIC数据库安装问题
  • ¥60 基于JTag协议开发Fpga下载器上位机,哪位大🐂有偿指导?
  • ¥20 全书网Java爬取数据
  • ¥15 怎么获取红包封面的原始链接,并且获取红包封面序列号
  • ¥100 微信小程序跑脚本授权的问题
  • ¥100 房产抖音小程序苹果搜不到安卓可以付费悬赏