5

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?

1 Answer 1

9

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

Sign up to request clarification or add additional context in comments.

4 Comments

Off-topic:Do you know about any similar complete module for PostgreSQL?
Don't forget to mark the answer. You can use something like this, I suppose.
Couple of my update values have comma (,) in them which is messing up the query. What do I do to get rid of that?
Are you sure you're using the functionality I have provided? The library should escape values as written here.

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.