title: HTML5 page logging with MySql database in Node.js with socket.io
body: <p>I tried the database login with HTML5+MySQL+Node.js+socket.io but failed. Please help.
  I want to login from HTML5 page to MySQL database with Node.js and socket.io</p>
  
  <p>I have a HTML5 page with 2 text box user name and password and a LogIn button.
  user want to login from HTML5 page with user/password send to server and the server receive the user/password and validate with mysql database and return the result.</p>
  
  <p><strong>My Code:
  server_test.js</strong></p>
  
  <pre><code>var io = require('C:/Program Files (x86)/nodejs/node_modules/socket.io/lib/socket.io');
  var socket = io.listen(8124);
  socket.sockets.on('connection',function(socket){
      socket.on('login', function(data, usr, pass){
          var mysql = require('mysql');
          var TEST_DATABASE = 'employee';
          var TEST_TABLE = 'tblusers';
          var client = mysql.createClient({
            user: 'root',
            password: 'secret10',
          });
  
          client.query('USE '+TEST_DATABASE);
  
          client.query(
            'SELECT name FROM '+TEST_TABLE+' WHERE user = '+usr+' AND password = '+pass,
            function selectCb(err, results) {
              if (err) {
                throw err;
              }
              //Emit a message to client
              socket.emit('retuLogIn',{username: results[0]['name']});
              client.end();
            }
          );
      });
          socket.on('disconnect', function(){
              console.log('Server has disconnected');
          });
  });
  </code></pre>
  
  <p><strong>client_test.html</strong></p>
  
  <pre><code>&lt;!doctype html&gt;
  &lt;html&gt;
      &lt;title&gt;WebSocket Client Demo [socket.io]&lt;/title&gt;
      &lt;script src="/json.js"&gt;&lt;/script&gt; &lt;!-- for ie --&gt;
      &lt;script src="http://localhost:8124/socket.io/socket.io.js"&gt;&lt;/script&gt;
      &lt;script&gt;
      function connect() {
  
          try
          {
              var socket = io.connect('http://localhost:8124/');
              socket.on("connect",function(){
                  document.getElementById('status').innerHTML ="Browser has connected to the app server";
              });
              socket.on('login', function (data, document.getElementById('txtUser').value, document.getElementById('txtPass').value) {
                  //alert(data.hello);
                  document.getElementById('status').innerHTML = 'Welcome '+data.username;
              });
          }
          catch(err)
          {
              document.getElementById('status').innerHTML = err.message;
          }
      }
      &lt;/script&gt;
      &lt;body&gt;
          &lt;h1&gt;WebSocket Client Demo&lt;/h1&gt;
          &lt;div&gt;&lt;p id="status"&gt;Enter user and password to Log-In&lt;/p&gt;&lt;/div&gt;
          &lt;label&gt;User :&lt;/label&gt;
          &lt;input id="txtUser" type="text" maxlength="10" /&gt;
          &lt;label&gt;Password :&lt;/label&gt;
          &lt;input id="txtPass" type="text" maxlength="10" /&gt;
          &lt;button id="connect" onClick='connect()'/&gt;Log-In&lt;/button&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Note :
  I am tried with above mention code but no success. I was tried to send the userid/password to server from client with socket.send('abcd', 'ab12'). able to send but can't receive in server.</p>
  
  <p>Please help me how to solve this issue. I am using MySQL database.</p>
  
  <p>As an update, when I apply Joel's suggestions, I still see an error when I try to run app.js in Node.js: </p>
  
  <pre><code>C:\Program Files (x86)\nodejs&gt;node "C:\Program Files (x86)\nodejs\app.js"    info  - socket.io started
  
  C:\Program Files (x86)\nodejs\app.js:6
      var client = mysql.createConnection({
                         ^ TypeError: Object #&lt;Object&gt; has no method 'createConnection'
      at Object.&lt;anonymous&gt; (C:\Program Files (x86)\nodejs\app.js:6:24)
      at Module._compile (module.js:449:26)
      at Object..js (module.js:467:10)
      at Module.load (module.js:356:32)
      at Function._load (module.js:312:12)
      at module.js:487:10
      at EventEmitter._tickCallback (node.js:238:9)
  
  C:\Program Files (x86)\nodejs&gt;
  </code></pre>
  
  <p>What could be wrong with this now?</p>