0

I am trying to fetch a bunch of values entered by the user using mysql like operator

In my get route in Nodejs I have

 console.log(req.query.key)
 pool.query("select FirstName from users where FirstName like %'" + req.query.key +"%'"

In my debug console I get

dav

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%'dav%'

I just ran this query in mysql workbench and it is working properly over there

select FirstName from users where FirstName like '%dav%'

MySQL Workbench returns

David

I am not sure why I get the error in Nodejs

Edit 1:

I am using the `mysql` package

app.js

    const mysql= require('mysql');

    const pool = mysql.createPool({
    connectionLimit : 10,
    host: keys.connection.host,
    user: keys.connection.user,
    password:keys.connection.password,
    database:keys.connection.database,
})

I am sure there is not issues with my connection because my other routes are working properly without error

2
  • What package are you using to run this query? I'm not familiar with "pool". Commented May 31, 2019 at 1:44
  • It's the mysql package. I edited my question to include the package setup Commented May 31, 2019 at 1:51

1 Answer 1

2

You need to put the quote character before the first %, not after it.

pool.query("select FirstName from users where FirstName like '%" + req.query.key +"%'"
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, I was working late. My answer was in my question. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.