title: NodeJS with mysql returning the first query result
body: <p>I have  a NodeJS with Express and mysql. I have put an Generic sql function so I call it for multiple times:</p>
  
  <pre><code>exports.selectCommand=function(table,cols,cond){
      var connection = mysql.createConnection({
          host     : hostname,
          database: databasename,
          user     : username
      });
      try{
          connection.query("SELECT "+cols+" As data from "+table+" where "+cond+" ", function(err, rows, fields) {
              if (err) return "sorry, an error accoured. Please try again later";
              if(rows.length!=0){
              cmd = rows[0].data.toString();
              }
          });
      }catch (e){console.log(e.message);}
      connection.end();
      return cmd; };
  </code></pre>
  
  <p>I send the query thorugh it but the problem it return the first query I send it.</p>
  
  <p>I've put the above function in another file and I call it:</p>
  
  <pre><code>var db=require('./db.js');
  ..........
  exports.psModel=function(){
      var cmddata="";
      cmddata=db.selectCommand("cmd","value","cmd='textbox'");
  }
  </code></pre>