title: node.js-MySQL COUNT the number of records
body: <p>I have following code. </p>
  
  <pre><code>var mysql = require('mysql');
  var connection = mysql.createConnection({
    host     : 'localhost',
    user     : 'root',
    password : '',
    database : 'test'
  });
  connection.connect();
  
  var userdata = '24';
  
  var sql = 'SELECT COUNT(*) FROM names WHERE age = ?'
  connection.query(sql, [userdata], function(err, rows, fields) {
    if (err) throw err;
    console.log('Query result: ', rows);
  });
  
  connection.end();
  </code></pre>
  
  <p>I want to get the total number of records from table 'names' where 'age' = 24. I receive the following on my node.js command prompt.</p>
  
  <pre><code>Query result:  [ { 'COUNT(*)': '14' } ]
  </code></pre>
  
  <p>My question is how to store this number 14 in a variable for further use. </p>
  
  <p>I know I can do this just by changing the 'sql' line to </p>
  
  <pre><code>var sql = 'SELECT * FROM names WHERE age = ?'
  </code></pre>
  
  <p>and then console line to</p>
  
  <pre><code>console.log('Query result: ', rows.length);
  </code></pre>
  
  <p>But still is there another way?</p>