Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set multiple fields in prepared queries in NodeJS

I am using 'mysql' library in Node

This is what i use instead of prepared statement, and works just fine :

 connection.query("update table set col1=? where col2=1",[val1],function(err,rows){
        //connection.release();
        if(!err) {
          //further code
          }
        });

However, this doesn't work for 2 unkowns :

    connection.query("update table set col1=? where col2=?",[val1],[val2],function(err,rows){
        //connection.release();
        if(!err) {
          //further code
          }
        });

The error message says "undefined is not a function". What am i doing wrong?

like image 687
user2473779 Avatar asked Nov 14 '15 11:11

user2473779


1 Answers

You need to define the values in a single array, like this:

connection.query("update table set col1=? where col2=?",[val1,val2],function(err,rows){
    //connection.release();
    if(!err) {
      //further code
      }
    });

All the necessary functionality can be easily found here: https://github.com/felixge/node-mysql/#preparing-queries

like image 129
Andrius Avatar answered Nov 14 '22 23:11

Andrius