Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SELECT WHERE IN in node-mysql

Does anyone know how to use SELECT WHERE IN in node-mysql?

I've tried the code below, but I get the following error message:

'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 ''(`PHP`,`apache`)'' at line 1'

This is my code:

whereIn = '(';
for ( var i in tagArray ) {
    if ( i != tagArray.length - 1 ) {
        whereIn += "`" + tagArray[i] + "`,";    
    }else{
        whereIn += "`" + tagArray[i] + "`"; 
    }
}
whereIn += ')';

console.log(whereIn);

client.query(
    'SELECT tag_id FROM tag WHERE tag_name IN ?',
    [whereIn],
    function(err, result, fields) {
        client.destroy();

        if (err) {
            throw err;
        }

        console.log(result);

        res.redirect('/');
    }
);
like image 633
Takehiro Adachi Avatar asked Jun 14 '12 20:06

Takehiro Adachi


1 Answers

You have to use IN (?) and NOT IN ?.

Any string manipulation may result in a SQL INJECTION backdoor.

like image 119
Daniele Vrut Avatar answered Sep 28 '22 08:09

Daniele Vrut