【问题标题】:Escaping characters in javascript for mysql在 javascript 中为 mysql 转义字符
【发布时间】:2016-09-18 07:16:31
【问题描述】:

我在将对象插入 mysql 表时遇到了一些问题。一个是 google lat/lng 对象(使用 VARCHAR):

JSON.stringify(coordinates)

[-90.02532485,35.04651167]

还有一些网址:

 mysqlEscape(aurl);

function mysqlEscape(stringToEscape){
    return stringToEscape
        .replace("\\", "\\\\")
        .replace("\'", "\\\'")
        .replace("\"", "\\\"")
        .replace("\n", "\\\n")
        .replace("\r", "\\\r")
        .replace("\x00", "\\\x00")
        .replace("\x1a", "\\\x1a");
};

 error on http://pbs.twimg.com/media/Ci5x4CZXAAAZCZc.jpg

如何插入这些?

【问题讨论】:

  • 您添加了反引号?你到底想用这个来完成什么?
  • 反引号用于表/字段名称,不是值。字符串值使用单引号或双引号。
  • 这个 JavaScript 是在浏览器中运行还是在服务器 (node.js) 上运行?
  • 我读过另一篇文章说它可能会解决它。将删除。运行 nodejs。
  • 您是如何尝试插入此对象的?你的 SQL 查询/代码是什么?

标签: javascript mysql


【解决方案1】:

要在 JS 中转义字符串,最好使用 encodeURIComponent 函数,而不是滚动自己的解决方案。

console.log(encodeURIComponent('http://pbs.twimg.com/media/Ci5x4CZXAAAZCZc.jpg'));

// output -> http%3A%2F%2Fpbs.twimg.com%2Fmedia%2FCi5x4CZXAAAZCZc.jpg

更多详情请查看encodeURIComponentdocumentation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-17
    • 2017-07-07
    • 2010-10-20
    • 2014-01-27
    • 2015-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多