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

305
Views
unexpected identifier on a mysql query in nodejs

I have the following code:

 connection.query("SELECT * FROM accounts_data WHERE new_username LIKE '%" + username + "%';", function (error, results, fields) {

});

and I am getting the following error

SELECT * FROM `accounts_data` WHERE `new_username` LIKE '%preloved_bys%'
                               ^^^^^
SyntaxError: Unexpected identifier
    at wrapSafe (internal/modules/cjs/loader.js:1054:16)
    at Module._compile (internal/modules/cjs/loader.js:1102:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
    at Module.load (internal/modules/cjs/loader.js:986:32)

what is wrong with the query? when i execute it via phpmyadmin it works just fine

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

i get success with do following :

var value = 'sc188203395acc255';
connection.query('SELECT * from emp where empid like ?', '%' + value + '%', ...)

Turn on debugging in the driver (pass debug:true as argument to mysql.createConnection ), it doesn't escape the percent sign

One More Way to Slove this Error :

"SELECT * FROM emp WHERE name empid " + connection.escape('%'+value+'%')

use connection.escape alternate way to do same things

One More Way to Solve this Error :

reinstall module on your project because sometimes error can be solved by reinstalling it

delete node_modules and package-lock.json and last run npm install

over 4 years ago · Santiago Trujillo Report

0

It seems there is nothing wrong with the following code.
but fyi, SyntaxError: Unexpected identifier occurs due to some reasons, mentioned below:

  1. SQL is case-sensitive language when you pass any parameter into it like 'new_username'. Recheck it should be correct.
  2. use of unwanted special character like ' or "
  3. using an invalid identifier.

    Give a try with the following code:
    let str = '%'+username+'%';
    let sql = "SELECT * FROM accouts_data WHERE new_username LIKE ?";
        
        connection.query(sql, str, function(err, result) {
            if(err) throw err
        });

Just in case, if it doesn't work, update your node_modules with `npm install`. Or just remove version number from all modules in `package.json` file like `"mysql": "^2.18.1"` just remove `^2.18.1`. and Then, Run:
npm install


It'll update every module with their latest version automatically.

over 4 years ago · Santiago Trujillo Report

0

Nothing wrong with the query, tested it with mysql and mysql2 and it gives the expected result. It must be related to nodejs, try removing node_modules directory and package-lock.json file and run npm install

over 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!