【发布时间】: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