D
D
Dmitry2020-02-29 15:11:57
MySQL
Dmitry, 2020-02-29 15:11:57

Error when looking up mysql2 node.js data?

Hello, I need to check if a record exists in a Mysql table using Node.JS.

To do this, I installed the Mysql2 module, connected everything, was able to connect to the database

const mysql = require("mysql2");

const connection = mysql.createConnection({
    host: "localhost",
    user: "root",
    database: "stream",
    password: ""
});


Next, all I need is just to see if there is a record with the fields I need
var uID = data.id;
        var uNick = data.nickname;
        var userD = [uID, uNick];
        connection.query("SELECT * FROM users WHERE ('Login', 'ID') VALUES('?',?)", userD, function(err, result){
            console.log(err);
            console.log(result);
        });


But Mysql2 complains about how I pass parameters:

code: 'ER_PARSE_ERROR',
errno: 1064,
sqlState: '42000',
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 'VALUES('1','Test')' at line 1"

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Cheremkhin, 2020-02-29
@Che603000

var userD = [uID, uNick];
Parameters are swapped. The SQL query comes first with Login (String) then ID (Number). You have the opposite. So he swears, about which he directly writes in an error.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question