Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

307
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda