Node.js and SQL to check a database for results
<p>I have this challenge that I had done with PHP, but wanted to see how it be done via node.js and SQL.</p>

<p>I am a beginner with this, and not sure at all if what I even did is legal, but am wanting to learn to do it in node; the challenge is this:</p>

<blockquote>
  <p>We would like a function that takes in the UserAgent of a browser as a string, and compares it to a list of approved browsers and versions  </p>
  
  <p>The database structure looks this:<br>
  -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -  -   -   -   -   -   -   -   -   -  -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -  -  -<br>
  | bigIntPKID | varchar(50) BrowserName | varchar(50) VersionNumber | bitGreaterVersions |<br>
  -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -  -   -   -   -   -   -   -   -   -  -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -  -  -<br>
  | &nbsp; &nbsp; &nbsp; &nbsp; 1 &nbsp; &nbsp; &nbsp; | &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Internet Explorer  &nbsp; &nbsp; &nbsp; &nbsp;| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 9 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; False &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|<br>
  -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -  -   -   -   -   -   -   -   -   -  -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -  -  -<br>
  | &nbsp; &nbsp; &nbsp; &nbsp; 1 &nbsp; &nbsp; &nbsp; | &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Internet Explorer  &nbsp; &nbsp; &nbsp; &nbsp;| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 10 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; False &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|<br>
  -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -  -   -   -   -   -   -   -   -   -  -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -  -  -<br>
  | &nbsp; &nbsp; &nbsp; &nbsp; 1 &nbsp; &nbsp; &nbsp; | &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Chrome &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 26 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; True &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;|<br>
  -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -  -   -   -   -   -   -   -   -   -  -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -  -  -<br>
  | &nbsp; &nbsp; &nbsp; &nbsp; 1 &nbsp; &nbsp; &nbsp; | &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Firefox &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  &nbsp; &nbsp; &nbsp; &nbsp;| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 22 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; True &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;|<br>
  -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -  -   -   -   -   -   -   -   -   -  -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -  -  -<br>
  bitGreaterVersions are any version greater than the version number is compatible.  <br><br>
  The data table expressed in human readable format would be “Our app is compatible with IE9,10, Chrome 26+ and Firefox 22+”
  <br><br>
  Create a function that will take a user agent as a string, and return true or false if it is compatible based on the above criteria.</p>
</blockquote>

<p>And then here is the node and SQL code (I hope)...</p>

<pre><code>var express = require("express");
var mysql = require("mysql");

var connection = mysql.createConnection({
    host: 'localhost',
    username: 'username',
    password: 'password',
    dbname: 'browsers'
});

var app = express();

// if there is an error connecting to the database
connection.connect(function(error) {
    if(error) {
        console.log('Cannot establish connection to database...');
        return;
    }
    console.log('Database connection established...');
});

// function to check browser compatibility
function getUserAgent(userAgent, versionNumber) {

    var isBrowserCompatible;

    // access the database
    app.get("/path-to-database", function(req, res) {
        // query for the required results
        connection.query('SELECT BrowserName, VersionNumber, bitGreaterVersions FROM userAgentTable', function(error, rows, fields) {
        connection.end();

            if(error) {
                console.log('Cannot find the results...');
            }

            isBrowserCompatible = false;

            // loop over the returned table values
            for(var i = 0; i &lt; rows.length; i++) {
                // if browser name and version number are equal to userAgent and versionNumber
                if(rows[i].BrowserName == userAgent &amp;&amp; rows[i].VersionNumber == versionNumber) {
                    // the browser is compatible
                    isBrowserCompatible = true;
                    break;
                  // if browser name is equal to userAgent but the version number is less than versionNumber
                } else if(rows[i].BrowserName == userAgent &amp;&amp; rows[i].VersionNumber &lt; versionNumber) {
                    if(rows[i].bitGreaterVersions == 'False') {
                        // the browser is not compatible
                        break;
                    } else {
                        // otherwise the browser is compatible
                        isBrowserCompatible = true;
                        break;
                    }
                }
            }
            return isBrowserCompatible;
        });
    });

    return isBrowserCompatible;

}
</code></pre>

<p>Hopefully someone can point me in the right direction. I'm mainly looking to see if the actual function can be written legally in the way that I have it, and if not, how to go about it, logic and all. Be gentle...</p>