0

I am very sorry; I would think this is an obvious one but could not find an answer in 5 hours worth of researching answers.

My second mysql query (using data from the first query) throws the following error.

errno: 1054, sql: 'SELECT ttday, ttfrom, tttill, ttuniqueid FROM timetables where ttname = Always',

When I query mysql manually or from code without variable and with Always in quotation marks "Always", the query computes correctly.

I seem to be unable to figure out how to pass the result of the first query (variable codetimetable.usertimetable) to the second mysql query in a computable (probably in quotation marks) way.

My code:


async function singledbentry(sql) {
    const [stripDbInfoLayer] = await pool.query(sql)
    return stripDbInfoLayer[0]
}


async function activetime(inputusercode) {
    var sql1 = 'SELECT usertimetable FROM users where usercode =' + inputusercode;
    let codetimetable = await singledbentry(sql1);
    console.log(codetimetable);
    console.log(codetimetable.usertimetable);

    var sql = 'SELECT ttday, ttfrom, tttill, ttuniqueid FROM timetables where ttname = ' + codetimetable.usertimetable;
    timetabledata = await singledbentry(sql);
    console.log(timetabledata);
    console.log(timetabledata.usertimetable);

}

activetime('4310')

Console errors:

Error: Unknown column 'Always' in 'where clause'

at PromisePool.query (C:\Node JS\Projects\user-management\node_modules\mysql2\promise.js:351:22)
at singledbentry (C:\Node JS\Projects\user-management\timeprogram.js:29:43)
at activetime (C:\Node JS\Projects\user-management\timeprogram.js:124:27)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {

code: 'ER_BAD_FIELD_ERROR', errno: 1054, sql: 'SELECT ttday, ttfrom, tttill, ttuniqueid FROM timetables where ttname = Always', sqlState: '42S22', sqlMessage: "Unknown column 'Always' in 'where clause'" }

1 Answers1

1

In Java you should change your code to the following to make it work, interestingly it's similar to this in Python :)

"SELECT usertimetable FROM users where usercode ='" + inputusercode + "';"

But you might also want to be careful of code injection. There is some details relating to that in this previously responded to post here:

How to input a NodeJS variable into an SQL query