P粉2129717452023-08-23 13:55:53
To answer the initial question and illustrate with an example, wrap the callback function in an anonymous function that immediately creates a "snapshot" scope containing the incoming data.
var ix=1; connection.query('SELECT 1', (function(ix){ return function(err, rows, fields) { console.log("ix="+ix); console.log(rows); }; })(ix));
For those like me who are just learning this concept, the last })(ix)); is the value of the outer var ix=1 which is passed to (function(ix){. If you would console.log("ix=" abc); is changed to console.log("ix=" abc);, then it can be renamed to (function(abc){.
fwiw (Thanks to Chris for the link, filling in the gaps to come up with the solution)
P粉4760461652023-08-23 11:39:22
If you are using node-mysql, please follow the instructions in the documentation:
connection.query( 'SELECT * FROM table WHERE id=? LIMIT ?, 5',[ user_id, start ], function (err, results) { } );
Code for properly escaping strings is also provided in the documentation, but using an array in a query call will automatically escape it for you.
https://github.com/felixge/node-mysql