【问题标题】:Making a dynamic query from node to a mySQL database从节点到 mySQL 数据库进行动态查询
【发布时间】:2019-12-17 15:23:44
【问题描述】:

我正在尝试使用 mysql npm 包从节点传递动态值。我很确定存在语法错误,但在我的一生中无法让查询正常运行。

这里是调用查询的函数:

function checkInventory() {
  connection.query('SELECT * FROM products WHERE item_id = ?'),[productID],
    function (error, results) {
      if (error) throw err;
      console.log(results);
    }
}

数据库连接已经建立,我之前在程序中读取了数据。但是,我不断收到错误消息,提示 mySQL 语法不正确。当我从工作台运行查询时,它工作正常。如何将我的productID 变量传递到查询中?

【问题讨论】:

  • productID 有什么值,错误信息是什么?
  • @Mohrn productID 是一个整数,我运行了一个 console.log 来仔细检查。错误信息是Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?' at line 1
  • @Mohrn 也尝试对productID 进行字符串化,同样的错误

标签: javascript mysql node.js npm


【解决方案1】:

问题在于括号。试试这个

function checkInventory() {
    connection.query('SELECT * FROM products WHERE item_id = ?', [productID],
    function (error, results) {
        if (error) throw err;
        console.log(results);
    })
}

【讨论】:

    猜你喜欢
    • 2018-04-02
    • 1970-01-01
    • 2015-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-29
    • 1970-01-01
    相关资源
    最近更新 更多