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 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动