Home >Database >Mysql Tutorial >How to Pass Custom Parameters to a MySQL Query Callback in Node.js?

How to Pass Custom Parameters to a MySQL Query Callback in Node.js?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-15 22:39:031112browse

How to Pass Custom Parameters to a MySQL Query Callback in Node.js?

Passing Parameters to MySQL Query Callback in Node.js

Problem

How can you pass custom parameters to a MySQL query callback in Node.js? In the code below, the intention is to pass the current value of "ix" used in the query to the callback:

...
for (ix in SomeJSONArray) {
    sql = "SELECT (1) FROM someTable WHERE someColumn = " + SomeJSONArray[ix].id;
    connection.query(sql, function (err, result) {
      ...
      var y = SomeJSONArray[ix].id;
    };
}

Solution

Using node-mysql, you can pass parameters to the query callback by including them as an array in the connection.query() function call. For example:

connection.query(
    'SELECT * FROM table WHERE>

This approach automatically escapes the strings passed as parameters, ensuring secure queries.

For further guidance on parameter escaping and other aspects of node-mysql, refer to the documentation at [https://github.com/felixge/node-mysql](https://github.com/felixge/node-mysql).

The above is the detailed content of How to Pass Custom Parameters to a MySQL Query Callback in Node.js?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn