Reputation: 28030
I have this mysql query written in node.js mysql and restify to be executed in a HTTP GET. https://github.com/felixge/node-mysql
var api_get_func = function (app, url_path) {
function respond(req, res, next) {
var id= req.query.id;
var limit = req.query.limit;
var query_str =
"SELECT table.sw_type, " +
"FROM users " +
"WHERE (id = ?) " +
"LIMIT ?"; //limit
var var_arr = [id, limit];
var query = connection.query(query_str, var_arr, function (err, rows, fields) {}
//SQL query ...
return next();
}
app.get(url_path, respond);
}
The HTTP GET URL is http://127.0.0.1/read_sw?id=51&limit=1
The error reported is that the query is not properly formed. The actual query looks like this;
SELECT table.sw_type,
FROM users
WHERE (id = 51)
LIMIT '1'
I think the problem is LIMIT '1'
. I think the solution is to make the query look like this;
SELECT table.sw_type,
FROM users
WHERE (id = 51)
LIMIT 1
How can the node.js code be modified?
Upvotes: 1
Views: 790
Reputation: 4618
This is not about mysql...
Anything in req.query is a string :
How about
var var_arr = [id, parseInt(limit)];
Upvotes: 2