I built an example that I hope will be useful, where only concatenate the value of a variable that I called id
to pass it to the query
The query I put it in quotation marks ""
and at the end said variable only concatenates it like this: +id
EXAMPLE
const express = require('express')
const app = express()
const mysql = require('mysql')
let connection = mysql.createConnection({
'server': 'localhost',
'user': 'root',
'password': 'secret',
'port': '3307',
'database': 'data'
})
connection.connect()
app.get('/all', function(req, res){
let id = 1
connection.query("SELECT * FROM users WHERE id = "+id, function(errors, results, fields){
console.log(results)
})
})
app.listen('8000', () => {
console.log("running")
})
At the end of the console, he throws me the following
[ RowDataPacket {
id: 1,
nameUser: 'gatito',
passwordUser: 'password',
statusUser: 1,
created_at: 2018-10-10T14:40:14.000Z } ]