简体   繁体   中英

nodejs how to retrieve variable value from mysql

I have a piece of code where I want to get the value of a variable from mysql database.

var PNOP;

  connection.query('SELECT pnop FROM MASTER1', [PNOP], function(err, results) {
  console.log(results);
 });

The connection query is working fine but the var PNOP is not getting updated.

The log file is showing the following entry:

[ { pnop: 7915.2 } ]

The value that I need to set as PNOP is 7915.2 which is what is in the mysql database.

What should I do to get the variable value?

The way nodejs-mysql works, the second parameter for the query contains the values of the fields you want to escape, and not the output parameters. The result of the query is in "results", as your log shows. what your code should be is:

var PNOP;

connection.query('SELECT pnop FROM MASTER1', [], function(err, results) {
  PNOP = results[0].pnop;
 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM