Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node | MySQL multiple WHERE with AND clause

Tags:

node.js

mysql

I am trying to do a statement in node using mysql package. I am trying to select from a table WHERE with a AND clause such as WHERE id = 1 AND course = 1 so far I've got as far as being able to SELECT but I'm getting an error, I can see the error though and it's that there is no AND when I am binding the id.

This is my current code.

let sql = 'SELECT * FROM users WHERE ?';

let values = {
    id: 1,
    course: 1
}

database.query(sql, values, (err, result) => {

}

This is the error I am getting...

SELECT * FROM users WHERE id = 1, course = 1

The query once it has been through node is missing the AND

Any help would be appreciated.


1 Answers

probably should look smth like this:

let sql = 'SELECT * FROM users WHERE id = ? AND course = ?';

database.query(sql, [1, 1], (err, result) => {

}

https://www.w3schools.com/nodejs/shownodejs_cmd.asp?filename=demo_db_where_placeholders

like image 81
Inna Tichman Avatar answered Oct 23 '25 22:10

Inna Tichman