节点mysql变量比较

时间:2017-08-26 12:19:15

标签: mysql node.js

嗨,我正忙着做一个小项目。我想要做的是将mysql行与变量进行比较,以便从数据库中获取用户steamid(函数winner_daily)(sry for my english :()。这里我的代码 的

var mysql = require('mysql');
var pool  = mysql.createPool({
  connectionLimit : 10,
  host            : 'localhost',
  user            : 'root',
  password        : '*****',
  database        : 'bet'
});

pool.query("SELECT `name` FROM `users` WHERE  levels >= 3 ", function (error, results, fields) {
  if (error) throw error;
  var rand = results[Math.floor(Math.random() * results.length)];

     winner_daily(rand);


});
function winner_daily(winner){

 console.log("And the winner is :",winner);
 pool.query(`SELECT steamid FROM users WHERE name = ${mysql.escape(winner)}`, function (error, row, fields) {
  if (error) throw error;

  console.log(row)
});

}

1 个答案:

答案 0 :(得分:0)

 console.log("And the winner is :",winner);

pool.query('SELECT * FROM `users` WHERE `name`=' + winner.name, function (error, row) {
      if (error) throw error;
      if(!row.length)   console.log('There is not any row'); return false;  

      console.log(row)
    });

您需要在查询中传递获胜者名称。