gpt4 book ai didi

mysql node js quest -- SELECT EXISTS 查询

转载 作者:搜寻专家 更新时间:2023-11-01 00:33:45 26 4
gpt4 key购买 nike

这个逻辑有什么问题?

var selectString = 'SELECT EXISTS(SELECT 1 FROM users WHERE user="'+username+'")';

var queryUserExists = connection.query(selectString);
var resultsLength = queryUserExists._results.length;

console.log(queryUserExists);

if(resultsLength > 0){
socket.write("fail user already exists"+"\r\n");

} else {
console.log('insert=');
var query = connection.query (
'INSERT INTO users '+
'SET user = ?, password = ?, token = ?',
[username, password, token]
);

socket.write("success"+"\r\n");
}

resultsLength 始终返回零。使用此查询的正确方法是什么?

返回对象:

sql: 'SELECT 1 FROM users WHERE user = \'a new user is my name\' ORDER BY user LIMIT 1',
typeCast: true,
nestTables: false,
_resultSet: null,
_results: [],
_fields: [],
_index: 0,
_loadError: null }

最佳答案

正确的代码:感谢 https://stackoverflow.com/users/722783/mahmoud-gamal对于优化的数据库查询!

connection.query("SELECT 1 FROM users WHERE user = '"+username+"' ORDER BY user LIMIT 1", function (error, results, fields) {
if (error) {
console.log(error);
socket.write("fail internal error"+"\r\n");
}
if (results.length > 0) {
console.log('fail');
socket.write("fail user already exists"+"\r\n");

} else {


console.log('insert');
var query = connection.query (
'INSERT INTO users '+
'SET user = ?, password = ?, token = ?',
[username, password, token]
);

socket.write("success"+"\r\n");


}

console.log(results);
});

关于mysql node js quest -- SELECT EXISTS 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14254197/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com