0

i want to use a variable in my Where clause like this :

con.query("SELECT * FROM vehicules WHERE "myvariable"=?",myothervariable,function (err, result)
    {
// some code
    });

it don't seems to work . is that even possible ?

EDIT

When i try this

con.query("SELECT * FROM vehicules WHERE 'myvariable'=?",myothervariable,function (err, result)

i got this as output

SELECT * FROM vehicules WHERE 'Modele'='X5' 

Modele is my right column name but how can i remove the ' ' ?

2
  • what lib do you use with mysql? Commented Oct 16, 2017 at 9:16
  • @IuriiDrozdov "mysql" is the lib's name Commented Oct 16, 2017 at 9:18

2 Answers 2

1

Yep, it's possible indeed.

db.query("SELECT * FROM vehicles WHERE ??=?",[your_column_name,your_var_from_your_code],function(error,results){.......})

And if you want to use multiple statements, go for :

db.query("SELECT * FROM vehicles WHERE ??=? AND ??=?",[column_name_1,code_var_1,column_name_2,code_var2],function(error,results){...})

This should theoretically work, I haven't the time to actually test it.

Sign up to request clarification or add additional context in comments.

9 Comments

Not working , when i do like this it show me this error : field your_var unknow
Replace it with your real table column name, I placed random placeholders.. both your_column_name and your_var_from_your_code are placeholders, change them both.
But my column name is a variable , i do this query dynamically whit a dropdown list value .
I've edited my answer, you should clarify your question just a bit, we misunderstood you. Use double question marks (??) for identifiers and one(?) for values.
Thanks Working ! how could i make it work if i replace WHERE ??=? with WHERE like ??=? with contains (??=%?% don't work)
|
0

You could try this:

con.query("SELECT * FROM vehicules WHERE ?",{myvariable: myothervariable},function (err, result)
    {
// some code
    });

1 Comment

Not working , field myvariable unknow (it's not taking my variable value but variable name)

Your Answer

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