0

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?

1 Answer 1

2

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;
 });
Sign up to request clarification or add additional context in comments.

4 Comments

The results[0].pnop is giving the correct value - when i log it. However, the var PNOP value does not seem to be getting set with this value.
When i log the value of PNOP it says undefined.
Yes - i tried it as it is - the var value was not getting set. so i added a line
console.log(results[0].pnop) - and in the log it was getting recorded as 7915.20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.