dongpao1083 2014-10-28 17:28
浏览 72
已采纳

转义出现在MySQL中的字符

I'm moving an HTML form from development (localhost) to production and initial testing has identified a problem with escape characters appearing in the production MySQL database that dont appear in the localhost database.

I'm using mysql_real_escape_string to escape characters in the form processing file and in the localhost database these are saved correctly and all you see is the apostrophe or quote symbol.

Once moved to production however, the form still processes correctly, but the database record now has the backslash escape character in front of the apostrophe or quote symbol. When the record is printed the backlash appears and this won't be acceptable.

Nothing else appears to be different, except localhost and production are running different versions of MySQL (localhost is running 5.5.24-log and production is running 5.0.95-log). I have no control over the production version as it is managed by the ISP, so I'm hoping this isn't the problem.

Hopefully this makes sense? Any help greatly appreciated.

  • 写回答

1条回答 默认 最新

  • dongpo5264 2014-10-28 17:35
    关注

    The solution to this is using stripslashes() before displaying the output

    $text = "Don\'t use mysql_* and use mysqli or PDO instead";
    echo stripslashes($text); // would echo it out without the backslash
    

    As an alternative, you can start using prepared statements which is the best practice for dealing with user input and database, which would also save you the hussle of having to escape all quotes, and stripping the backslashes before displaying them.

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

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 关于无人驾驶的航向角
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了