Hi I am trying to run a query but getting the following error: Nodej Mysql2
query
tableName = "some_table"
id = "some_id";
const readForwardedMessageDetails = pool.execute('SELECT * FROM ?? WHERE `id` = ? LIMIT 1',
[tableName, id],
function(err, row) {
if (err) {
console.log(err)
return
} else {
}
code: 'ER_PARSE_ERROR',
errno: 1064,
sqlState: '42000',
sqlMessage: "You have an error in your SQL syntax; check the manual
that corresponds to your MariaDB server version
for the right syntax to use
near '?? WHERE `id` = ? LIMIT 1'
at line 1 ",
sql: 'SELECT * FROM ?? WHERE `id` = ? LIMIT 1'
Was trying from here : Nodejs-Mysql Query table name as a variable
Also use of ?? double question marks for tablename is given here : https://github.com/mysqljs/mysql#preparing-queries
MySql2 suggests it is mostly compatible with mysql so tried the answers above. https://github.com/sidorares/node-mysql2#history-and-why-mysql2 : MySQL2 is mostly API compatible with mysqljs and supports majority of features. MySQL2 also offers these additional features (hence the inference)
SELECTworks in MySQL (or any RDBMS, for that matter) - it would behoove you to research more closely what the syntax for such a query should be. Cursory research would yield you the idea that these question marks should be replaced with the name of the table you're attempting to query from your database. Would you care to show us how exactly you arrived at the conclusion that your query as-is should work? How to Askmysql2in your question, but both your links refer to themysqlpackage - which are you using? I cannot find any mention of this functionality being provided in themysql2package; nor would I recommend such a design where you are reading dynamically from a table at runtime (such designs are usually indicative of more problematic design decisions elsewhere).mysql2- yet both the Stack Overflow thread and the GitHub documentation are referring to a completely different package - it's not clear how ever you reached the conclusion that they are interchangable 1:1.mysql2at all. It's also not clear how it would be relevant if I did in fact edit my comment, anyway. Finally, you still have not addressed the fact that it appears you're referencing documentation for a completely different package (mysql) than you are actually employing in your script (mysql2).