In JavaScript, the string looks like this:
`SELECT * from stores WHERE ${field_name} = 0 `;
When parsed in SQL, it looks like this:
SELECT * from stores WHERE someFieldName = 0 `;
Where ${field_name}
is returning whatever field_name
is, so in SQL, this is a syntax error where "someFieldName" is not defined.
You need to put quote marks around your query:
const get_non_showing_stores = (field_name) => {
const connection = mysql_conn();
const query = `SELECT * from stores WHERE '${field_name}' = 0;`;
connection.query(query, (err, results) => {
return results;
});
};
Edit: It seems like you're trying to return the result of selection field_name
. The "field_name" you are calling in the function, mat not be defined in your SQL Database.
Also, you are returning the value from connection.query
, inside the function. You can not return a value from a child function / event listener.
You will need to call connection.query
inside the place of code you are calling the function get_non_showing_stores()
, then do what you want with the return value.
Another good SQL syntax tip: You should capitalize all built-in SQL syntax. Right here:
SELECT * from stores WHERE ${field_name} = 0;
^^^^
Missing capitalization
And always should put a semicolon at the end of syntax:
SELECT * FROM stores WHERE ${field_name} = 0
^
Missing semicolon