Languages

Express.js / MySQL - query works in HeidiSQL and doesn't work in express.js

0 points
Asked by:
Joanne-Senior
500

I have the following problem: Express.js throws an error with SQL query which works fine in HeidiSQL.

My query:

const dbQuery = `SELECT * FROM users WHERE username='${userToLogin.username}' AND password='${userToLogin.password}'; INSERT INTO login_history (login_time) VALUES ('${getDateInFormat()}');`;

Error:

code: 'ER_PARSE_ERROR',
errno: 1064,
sqlMessage: "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INSERT INTO login_history (login_time) VALUES ('2021-08-31 00:17:07')' at line 1",
sqlState: '42000',
index: 0,
sql: "SELECT * FROM users WHERE username='admin' AND password='admin'; INSERT INTO login_history (login_time) VALUES ('2021-08-31 00:17:07');"

And when I copy the following part from "sql" section to HeidiSQL it works:

SELECT * FROM users WHERE username='admin' AND password='admin'; INSERT INTO login_history (login_time) VALUES ('2021-08-31 00:17:07');
1 answer
0 points
Answered by:
Joanne-Senior
500

The problem could be multiple-statement query. Make sure if your connection includes the following line:

multipleStatements: true

Example:

mysql.createConnection({multipleStatements: true});
0 comments Add comment
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join