1

I am using the mysql module with my Node.js/Express application.

When I query MySQL for data it doesn't have, it shows errors. Thats what Im expecting. But if I use connection.escape() on the data passing through, even though theres suppose to be an error, it acts likes nothing happened and continues down the code. Why?

Below is the code I have. Remember I am passing in data that doesn't exist. I am expecting the error, I want it to happen.

Below does what I expect. The id I passed via POST is not in the database, thus console logging: ERROR

app.post('/check', function(req,res) {

    connection.query('SELECT * FROM category where id="' + req.body.id + '"', function(err,rows,fields) {

        if(err) { 
            console.log('ERROR'); 
        } else {
            console.log('SUCCESS');
            }           


    });

});

Below I have added the connection.escape() feature. Now when it runs, even though the data is not in the database it console logs SUCCESS.

app.post('/check', function(req,res) {

    connection.query('SELECT * FROM category where id=' + connection.escape(req.body.id) + '', function(err,rows,fields) {

        if(err) { 
            console.log('ERROR'); 
        } else {
            console.log('SUCCESS');
            }           


    });

});

Why is this happening?

This is the error I get when I run the first example of code. The 2nd example I dont get any errors until I reach an empty property that is related to the query. Since there were no results and I couldnt stop the application when the error is suppose to occur

[Error: ER_BAD_FIELD_ERROR: Unknown column 'fsaf' in 'where clause']
 code: 'ER_BAD_FIELD_ERROR',
 errno: 1054,
 sqlState: '42S22',
 index: 0 }

1 Answer 1

2

because there is no error - your query is valid and finished successfully. You received empty result set and this is expected and valid behaviour

In your first example the result of string concatenation is probably invalid sql, please post the error you have

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

1 Comment

You are right about the concatenation being invalid. I fixed it. It was only an error here I had it correct in my application. I posted the error I get when I expect it. I get no errors when I use escape().

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.