Reputation: 6349
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);
});
Upvotes: 3
Views: 5696
Reputation: 14982
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}`);
});
Upvotes: 8
Reputation: 2564
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.
Upvotes: 1