Search code examples
mysqlnode.jsnode-mysql

node-mysql returns column names instead of values


I have a mysql query like this: 'SELECT ? FROM tablename' with an args array like this: ['id']. When I call db.query(sql, args) the result is an array with ['id', 'id', 'id'] for every row in the database table instead of having the values from the database in it [1,2,3]. If the column names are included in the sql string it works just fine. I really dont know why this happends. Is there someone else having issues like this and has a solution for it?


Solution

  • You can parameterize column names with the ?? placeholder as per the documentation:

    db.query('SELECT ?? FROM tablename', [ [ 'id', 'id', ... ] ]);