php - php 的 mysql_real_escape_string() 的等效 javascript 代码是什么?

标签 php javascript mysql sql-injection

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/

相关文章:

php - 使用 order by 出现意外结果

php - Mysqli 查询没有返回任何结果

php - 如何在PHP中动态显示创建的MySql Table内容

c# - 更新网站上文本的最佳方法

javascript - Iron-Router:显示列表和单篇文章的一种路由

javascript - 基于浏览器的 session 计时器

javascript - 输入字段未附加到 td

mysql - Oracle Golden Gate 同步 Oracle - mysql

php - SQLite v2.8 (PHP5) 中列的元数据

html - 我如何在不打开新页面进行编辑的情况下在 jsp 页面中编辑我的表,它应该反射(reflect)在数据库中?