apr_dbd_escape

来源:互联网 发布:中电海康研究院 知乎 编辑:程序博客网 时间:2024/05/29 18:55

最近发现 apr_dbd_escape将  ‘ (单引号)转义成   \\'   而 apr_dbd_pvquery 将 \\' 解释成 \'  于是 当你想执行

insert  into account ( username ) values ( "abc'def" )

时,用apr_dbd_escape转义后会变成插入 abc\'def, 而不用apr_dbd_escape转义反而是期待的结果,插入了 abc'def

以上现象与我先前的理解有出入。

1. 我知道 escape_string_for_mysql 将会对 ‘ , “ , \n , \r  , \ 进行转义。从源码看来, apr_dbd_escape 调用的就是escape_string_for_mysql  不应该出现 ' 转成 \\' , 应为 \'

2. apr_dbd_pvquery  在执行一些带有‘ 或 “ 语句, 应该转义。 但结果发现不转义反而出现正确的结果。


经查,我在apr_dbd_pvquery中用了prepared statement. 在这种情况下,apr_dbd_pvquery  在执行一些带有‘ 或 “ 语句, 无需转义。详见:

http://stackoverflow.com/questions/2003572/how-can-i-escape-single-and-double-quotes-in-sql-prepared-statement

里面某牛人(Bill Karwin)如是描述:

If the DBI is "emulating" prepared statements by interpolating the variables into the query string, then DBI should handle the correct escaping logic so you don't have to. Let the experts (those who write and test DBI) worry about how to do it.

我先前理解 2 是错误的。