0

I have a get API in nodejs , which gets string of variables as query string ( delimited by ",") as follows

const mac = req.query.mac; 
console.log(mac); // 00:11:22:33:FF:EE,11:11:22:33:FF:EE
var sql =  mysql.format("SELECT * FROM user_email WHERE macId IN ?",[mac]);
        connection.query(sql, function(err, row) ...{ ... .. }

But i am getting errors

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 ''00:11:22:33:FF:EE,11:11:22:33:FF:EE'' at line 1",
  sqlState: '42000',
  index: 0,
  sql: "SELECT * FROM user_email WHERE macId IN '00:11:22:33:FF:EE,11:11:22:33:FF:EE'"
}

Someone please help !!

5
  • ''00:11:22:33:FF:EE,11:11:22:33:FF:EE'' additional quote is getting added Commented Jul 8, 2020 at 11:00
  • But i haven't added nay extra , how can handle this ? Commented Jul 8, 2020 at 11:09
  • mysql.escape() try with this ? Commented Jul 8, 2020 at 11:12
  • "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 ''\\'00:11:22:33:FF:EE,11:11:22:33:FF:EE\\''' at line 1", Commented Jul 8, 2020 at 11:33
  • Above result after using mysql.escape Commented Jul 8, 2020 at 11:34

1 Answer 1

1

your mistake is in converting mac from string to array

[mac]

won't convert it to a string. try this instead:

const mac = req.query.mac; 
const macArr = mac.split(',');
var sql =  mysql.format("SELECT * FROM user_email WHERE macId IN (?)", macArr);
connection.query(sql, function(err, row) ...{ ... .. }
Sign up to request clarification or add additional context in comments.

4 Comments

STILL AS THIS ------------------------------------------------------------- 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 ''00:11:22:33:FF:EE'' at line 1", sqlState: '42000', index: 0, sql: "SELECT * FROM user_email WHERE macId IN '00:11:22:33:FF:EE'" }
Only one mac is passed to query now
Its solved now, used brackets on ? as this (?) solved the issue
oh sure my mistake, I will edit my answer. thanks for sharing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.