3

I want to store the result of count (ex:- 5) in MySQL query to a variable, but it is storing the whole JSO string as [{"COUNT(*)":8}], so How to store value 8 in my variable row?

My code in NodeJS:

connection.query('SELECT COUNT(*) FROM issues',
            function(err, rows){
            var row=JSON.stringify(rows);
            console.log(row);

            });
1
  • use alias: SELECT COUNT(*) as cnt ... Commented Jul 31, 2015 at 14:14

2 Answers 2

8

Use sql alias:

connection.query('SELECT COUNT(*) AS count FROM issues', (err, rows) => {
    const count = rows[0].count;
    // const count = rows[0]['COUNT(*)']; // without alias
    console.log(`count: ${count}`);
});
Sign up to request clarification or add additional context in comments.

Comments

1

Simply extract the value from the rows object by key. rows[0]['COUNT(*)'] should work. Do that before you stringify it.

EDIT: As the comment below pointed out, rows is an array. Edited answer.

1 Comment

You forgot about rows is an array :)

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.