How to input a NodeJS variable into an SQL query

后端 未结 2 1763
眼角桃花
眼角桃花 2020-12-10 07:33

I want to write an SQL query that contains a NodeJS variable. When I do this, it gives me an error of \'undefined\'.

I want the SQL query below to recognize the

2条回答
  •  清歌不尽
    2020-12-10 08:04

    If you are using > ES6 :

    connection.query(`SELECT * FROM arrivals WHERE flight = ${flightNo}`, function(err, rows, fields) {
    

    If you are < ES6 :

    connection.query("SELECT * FROM arrivals WHERE flight = " + flightNo, function(err, rows, fields) {
    

    Please note that this is VERY BAD practice as you will be vulnerable to SQL-injection attacks.

提交回复
热议问题