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.

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

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改