8

SELECT WHERE INnode-mysqlでの使用方法を知っている人はいますか?

以下のコードを試しましたが、次のエラーメッセージが表示されます。

'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'

これは私のコードです:

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('/');
    }
);
4

6 に答える 6

18

IN (?)と NOTを使用する必要がありIN ?ます。

文字列を操作すると、SQL INJECTION バックドアが発生する可能性があります。

于 2015-03-06T09:21:35.670 に答える
2

バッククォートを使用するのではなく、文字列を引用する必要があります。

whereIn = '(';
for ( var i in tagArray ) {
    if ( i != tagArray.length - 1 ) {
        whereIn += "'" + tagArray[i] + "',";    
    }else{
        whereIn += "'" + tagArray[i] + "'"; 
    }
 }
whereIn += ')';
于 2012-06-14T21:02:11.533 に答える
-1

実用的なソリューション:

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

        if (err) {
            throw err;
        }

        console.log(result);

        res.redirect('/');
    }
);

tagArray を手動で引用符で囲む必要はありません。mysql モジュールによってエスケープされます。

于 2016-10-25T13:50:18.907 に答える