title: Running Multiple Queries in Nodejs MySQL
body: <p>I'm using the <a href="https://github.com/felixge/node-mysql/" rel="nofollow">mysql library</a> for node.js applications.  I'm new to MySQL in general, but I've been able to successfully run a single query and return the data to my webclient.  The problem is it only seems to run once (before throwing an error).  Here's the code running on my server:</p>
  
  <pre><code>//connect to MySQL server
  connection.connect(function(err) {
    if (err) {
      console.error('error connecting: ' + err.stack);
      return;
    }
    console.log('connected as id ' + connection.threadId);
  });
  
  //do MySQL query
  connection.query('SELECT * FROM `mydatabase` WHERE `tag` = \"' + data.AppID + '\" AND `time` BETWEEN \"' + t_from.toISOString() + '\" AND \"' + t_to.toISOString() + '\" ORDER BY ID DESC', function (error, results, fields) {
    //return the results to webclient here.
  });
  
  //close the connection
  connection.end();
  </code></pre>
  
  <p>So, basically every time I click a button, it sends a request to the server (using socket.io) and runs the query.  It first creates the connect, runs the query (and returns the data to the webclient), and closes the connection. This works the first time I click the button, but any subsequent clicks returns nothing. My understanding was that I need to open and close the connection every time I run a query (and not leave it open)... but can someone tell me what I'm doing wrong?</p>