mysql2 prepared statement

I wanted to ask you which way is the best for prepared statement in mysql2 First:
try {
const [rows] = await db.execute('SELECT * FROM account WHERE login = ?', [username]);

if (rows.length > 0) {
console.log("error");
return;
}

const [result] = await db.execute('INSERT INTO account (login, password, discord_id) VALUES (?, ?, ?)', [username, password, interaction.user.id]);

console.log("succesfull");
} catch (err) {
console.error('no DB:', err);
}
try {
const [rows] = await db.execute('SELECT * FROM account WHERE login = ?', [username]);

if (rows.length > 0) {
console.log("error");
return;
}

const [result] = await db.execute('INSERT INTO account (login, password, discord_id) VALUES (?, ?, ?)', [username, password, interaction.user.id]);

console.log("succesfull");
} catch (err) {
console.error('no DB:', err);
}
Second:
const checkUserQuery = 'SELECT * FROM account WHERE login = ?';
const checkUserParams = [username];
try {
const [result] = await connection.execute(checkUserQuery, checkUserParams);

if (result.length > 0) {
console.log("error");
return;
}

const createUserQuery = 'INSERT INTO account (login, password, discord_id) VALUES (?, ?, ?)';
const createUserParams = [username, password, interaction.user.id];

await connection.execute(createUserQuery, createUserParams);

console.log("succesfull");
} catch (err) {
console.error('no DB:', err);
}
}
const checkUserQuery = 'SELECT * FROM account WHERE login = ?';
const checkUserParams = [username];
try {
const [result] = await connection.execute(checkUserQuery, checkUserParams);

if (result.length > 0) {
console.log("error");
return;
}

const createUserQuery = 'INSERT INTO account (login, password, discord_id) VALUES (?, ?, ?)';
const createUserParams = [username, password, interaction.user.id];

await connection.execute(createUserQuery, createUserParams);

console.log("succesfull");
} catch (err) {
console.error('no DB:', err);
}
}
2 Replies
d.js toolkit
d.js toolkit10mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by staff
monbrey
monbrey10mo ago
#other-js-ts This is not a djs question
Want results from more Discord servers?
Add your server