0

Hello I am trying to do a request in node.js with MySQL that is similar to

SELECT * FROM table_name WHERE tag="some_word" AND username="some_username"

I have not been able to figure out how to make a request work when wanting to use more than one variable at once. I can get my requests to work when doing something like

SELECT * FROM table_name WHERE username="some_username"

but I can't seem to find any answers on how to do this when adding the AND to the query. Any suggestions? Thank you in advance!

6
  • Possible duplicate of SELECT not working in node.js Commented Sep 5, 2016 at 13:03
  • @Roberrrt I'm sorry but I'm not sure how this is a possible duplicate of the post you are comparing it to. Can you explain why you think that it might be? It doesn't seem to me that he is asking the same question of how to use multiple variables in one request using SELECT. Commented Sep 5, 2016 at 13:10
  • I'm, not really sure why that URL appeared there, i'd swear that that was not the URL I put there. Either way, on to your question: What have you tried, where is your code, do you have some sort of example? Commented Sep 5, 2016 at 13:11
  • 1
    @Roberrrt I found an answer. Thank you for your help! Commented Sep 5, 2016 at 13:28
  • Well, my help, my help, Dylan helped you :). I'll upvote his answer, if you feel it answered your question, you should accept it by pressing the green icon on the left side ;). Commented Sep 5, 2016 at 13:32

1 Answer 1

4

This may be something that you want. Just put whatever variables you want into the filter array and it will insert into the SELECT query.

var some_word;
var some_username;
var queryString = "SELECT * FROM table_name WHERE tag= ? AND username= ?;"
var filter = [some_word, some_username];

connection.query(queryString, filter, function(err, results) {
   //process results
});

More info at: https://github.com/mysqljs/mysql#escaping-query-values

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

2 Comments

Thank you @Dylan this is what I was looking for!
@PaulB. Glad that it helps. Happy Coding :)

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.