mysql_real_escape_string()
的等效 javascript 代码是什么?
最佳答案
基于PHP documentation方法的这将做大致相同的事情。然而,PHP 中的 mysql_real_escape_string 方法已被弃用。
function mysqlEscape(stringToEscape){
return stringToEscape
.replace("\\", "\\\\")
.replace("\'", "\\\'")
.replace("\"", "\\\"")
.replace("\n", "\\\n")
.replace("\r", "\\\r")
.replace("\x00", "\\\x00")
.replace("\x1a", "\\\x1a");
}
不幸的是,根据 5.6 mysql API docs,这并不是 mysql_real_escape_string 所做的。 .它没有考虑字符编码等因素。
即使您只使用前 2 个替换,上述方法也可能会满足您的需求。
function mysqlEscape(stringToEscape){
return stringToEscape
.replace("\\", "\\\\")
.replace("\'", "\\\'");
}
根据 mysql 文档。
MySQL requires only that backslash and the quote character used to quote the string in the query be escaped. mysql_real_escape_string() quotes the other characters to make them easier to read in log files
关于php - php 的 mysql_real_escape_string() 的等效 javascript 代码是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5191062/