Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

185
Views
Assign variables to query mysql on NodeJS

Hi I have some problem on nodejs to get data on mysql

My question is 'How can I add var n into "%drawing eye%" to get data on database?'

  function showResult(req, res){
                var n = req.query.query
                mysql_conn.query('SELECT query_text FROM catalogsearch_query WHERE query_text LIKE "%drawing eye%" ORDER BY popularity DESC LIMIT 0 , 10', function(error, rows){
                res.render('result.html',{result:n , related: rows.map(row => row.query_text)})
                })
        }
about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

do something like this LIKE "%' + n + '%" ORDER BY

function showResult(req, res) {
    var n = req.query.query
    mysql_conn.query('SELECT query_text FROM catalogsearch_query WHERE query_text LIKE "%' + n + '%" ORDER BY popularity DESC LIMIT 0 , 10', function(error, rows) {
        res.render('result.html', {
            result: n,
            related: rows.map(row => row.query_text)
        })
    })
}
about 4 years ago · Santiago Trujillo Report

0

The mysql module supports queries with placeholders, where those placeholders get replaced by variables you pass (properly-escaped, to prevent SQL injections):

function showResult(req, res) {
  var n = req.query.query;
  mysql_conn.query(`
    SELECT   query_text
    FROM     catalogsearch_query
    WHERE    query_text LIKE ?
    ORDER BY popularity DESC
    LIMIT    0, 10`,
    '%' + n + '%',
    function(error, rows) {
      if (err) return res.sendStatus(500);
      res.render('result.html',{result:n , related: rows.map(row => row.query_text)})
    }
  )
}

The ? after LIKE is the placeholder, which will get replaced by an escaped version of '%' + n + '%'.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!