mysqljs / mysql

A pure node.js JavaScript Client implementing the MySQL protocol.
MIT License
18.27k stars 2.52k forks source link

Incorrect Sql syntax escaping multiple updates in single query #1831

Closed Holo-Buckshot closed 6 years ago

Holo-Buckshot commented 6 years ago

Dear lord that is a long title but i wanted to make sure i properly described the issue.

connection.query('UPDATE users SET foo = ?, bar = ?, baz = ? WHERE id = ?', ['a', 'b', 'c', userId], function (error, results, fields) {
  if (error) throw error;
  // ...
});

straight from the github page This doesn't work with sql and throws an error. I tried it with my database values

con.query('UPDATE settings SET admins=?, mods=?, bypass=?, autowarn=?, panic=?, verify=?, censor=?, prefix=?, panicd=?, lang=? WHERE guildref= 343438623728861184', [values], function(err, result) {
      if (err) throw err;
      console.log("Rows created: " + result.affectedRows);
    });

and it throws an error see below I also checked the sql syntax on this website https://www.piliapp.com/mysql-syntax-check/ both mine and the example and it still gets mad at that first question mark.

I'm not exactly sure if i'm somehow doing this wrong or maybe the documentation is incorrect. I could probably live without it. This happens to be a special case where i'll be needing multiple inputs for an update call, but it would be nice to know if something is wrong or i'm just being an idiot.

0|gaius    | /home/holo/gaius/node_modules/mysql/lib/protocol/Parser.js:80
0|gaius    |         throw err; // Rethrow non-MySQL errors
0|gaius    |         ^
0|gaius    | Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?, bypass=?, autowarn=?, panic=?, verify=?, censor=?, prefix=?, panicd=?, lang=?' at line 1
0|gaius    |     at Query.Sequence._packetToError (/home/holo/gaius/node_modules/mysql/lib/protocol/sequences/Sequence.js:52:14)
0|gaius    |     at Query.ErrorPacket (/home/holo/gaius/node_modules/mysql/lib/protocol/sequences/Query.js:77:18)
0|gaius    |     at Protocol._parsePacket (/home/holo/gaius/node_modules/mysql/lib/protocol/Protocol.js:279:23)
0|gaius    |     at Parser.write (/home/holo/gaius/node_modules/mysql/lib/protocol/Parser.js:76:12)
0|gaius    |     at Protocol.write (/home/holo/gaius/node_modules/mysql/lib/protocol/Protocol.js:39:16)
0|gaius    |     at Socket.<anonymous> (/home/holo/gaius/node_modules/mysql/lib/Connection.js:103:28)
0|gaius    |     at emitOne (events.js:115:13)
0|gaius    |     at Socket.emit (events.js:210:7)
0|gaius    |     at addChunk (_stream_readable.js:266:12)
0|gaius    |     at readableAddChunk (_stream_readable.js:253:11)
dougwilson commented 6 years ago

The reason you got that error is because your query has 10 question marks, but you only provided one value: whatever the values variable is. If that variable actually is an array of those 10 values, just remove the surrounding [ and ]. If that doesn't work for you, can you provide what the contents of the values variable is?

Holo-Buckshot commented 6 years ago

alright let me do that, the values is an array but it's a rather large amount of code that goes into building that array since i'm reading off json data, transforming objects into array data then shoving it into a database, really annoying....

Holo-Buckshot commented 6 years ago

Looks like that was the issue, thanks, i guess i really was being a total dumbass. However now i have more problem, awesome :P oh well, coding is just an endless line of bugs anyways.

dougwilson commented 6 years ago

No problem!