I run via two for-loops through a MySQL table "Book" to get the values:
If I use explicitly the column name "Betrag" to get the value (e.g. res[i].Betrag
), it works! When I use for the column name a variable ('colName'
), it issues an error message.
How can I get the values of a MySQL table without typing the column name in the code?
Thanks for your help, M@trix
Value.toString()
app.get('/', function (req, res) {
//Build connection to MySQL database
var mysql2 = require('mysql');
var db2 = mysql2.createPool({
host: "127.0.0.1",
user: "...",
password: "...",
database: "DataRobot"
});
db2.query('SELECT * FROM Book', function (error, results, fields) {
if (error) throw error;
});
function setResHtml(sql, cb){
db2.getConnection((err, con)=>{
if(err) throw err;
con.query(sql, (err, res, cols)=>{
if(err) throw err;
for(var i=0; i<res.length; i++){
for(var j=0; j<3; j++) {
var colName= cols[j].name;
console.log("res[i].Betrag:");
console.log(res[i].Betrag); // shows the value
console.log("res[i].colName:");
console.log(res[i].colName); // shows an error message!!!!
};
}
con.release();
return cb(table);
});
});
}
Get entries via a variable for the column name.