从Mysql查询更新检索到的数据? Node.js node-mysql

时间:2016-05-14 18:59:16

标签: javascript mysql node.js node-mysql

我目前正在开发一个socket.io-client / socket.io登录系统,问题是当我启动服务器时,执行查询,检索数据库中的当前数据,如果用户注册它们不是能够登录,直到服务器重新启动,如果我使用:

 SELECT * FROM mytable ORDER BY id DESC LIMIT 1

然后,只有刚注册的用户才能登录,而不是已注册的用户,我该如何解决此问题?

我知道,当用户名和密码从客户端提交到服务器时,会执行包含Mysql代码的函数,逻辑上我认为这会为每个新连接再次运行查询,但事实并非如此。

我的代码:

服务器:

console.log("Berdio - A Awesome socket server for everyone!")
console.log("Created with Love, by OzzieDev! Enjoy the awesomeness!")

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
function dosql(){
var mysql = require('mysql')
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'root',
database : 'Berd'
})
connection.query('SELECT * FROM Berd_Data', function(err, rows, fields,     result){
if (err) throw err;
var usrname = rows[0].Username
var psword = rows[0].Password
global.usrname = usrname 
global.psword = psword
});
} 
io.on('connection', function(socket){
})
 socket.on('user-name', function(data){
 dosql()

  socket.broadcast.emit('user-connected',data.u);

 console.log(u,p)
 var u = data.u 
 var p = data.p 
 console.log(u,p + " " +  "emitted Data");
  if (u === global.usrname && p === global.psword){
    socket.emit('AuthGood')
  }else{
    socket.emit('AuthFail')
  }
socket.on('msg',function(data){
var message =  data.message
if (typeof message !== "undefined"){
  socket.broadcast('newmsg',msg) 
}
   if(message === "quit"){
 process.exit();
}
})


});
});

http.listen(3000, function(){
console.log('listening on *:3000');
});

客户:

  var socket = require('socket.io-client')('http://127.0.0.1:3000');
  socket.on('connect_error', function(){
  console.log('Failed to establish a connection to the servers, or lost  connection');

 return process.exit();     
 });
 var prompt = require("prompt-sync")()
  console.log("Your Ip has been logged, but encrypted. Please see website for   more info")
 var news = "Add news: Will be from database. " 
 var username = prompt("Username>: ")
 var password = prompt("Password>: ")

 if (typeof username, password !== "undefined"){
 socket.emit('request')
 socket.emit('user-name', { u: username, p: password });
 }
socket.on('AuthGood',function(socket){
console.log("Your details have been authenticated, hello again!")
var message = prompt("message>: ")
})
socket.on('AuthFail',function(socket){
console.log("Your details failed to authenticate, Wrong Pass/Username    combination, quitting!")
return process.exit();
 })


if (typeof message !== "undefined"){
socket.emit('msg',{m: message})
}
socket.on('user-connected',function(data){
var userconn = data.username 
console.log(username +" " +"Has joined us!")

})
socket.on('newmsg',function(data){
var newmsg = data.msg 

console.log("-----------------------------------") 
console.log(username+">:" + " " +msg)
})

1 个答案:

答案 0 :(得分:0)

使用MySql查询:

SELECT * FROM Berd_Data WHERE `Username` = ?', [u]

修正了问题。