What is the correct way to make this MySQL function in Node?

Viewed 27

I'm trying to make this function that returns false if it cannot find the record in the SQL DB, and true if the record exists, in this case, the record DOES exist, but it doesn't change the variable called "returnvariable" to true when it enters that if statement. However, it DOES do the console.log("Case row found"). Can anyone help me out understand why this doesn't work? This is what it returns in the log (below is the actual code):

false
MySQL successfully ended
MySQL successfully connected
Case row found



var mysql = require('mysql'); // mysql

var con = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "****",
    database: "world"
});

con.connect( function ( err ) {
    if (err) {
        throw err;
    } else {
        console.log("MySQL successfully connected");
    }    
  }
);

function check_sql(what, table, column, likewhat, limit){
    var returnvariable = false;
    con.query(`SELECT ${what} FROM ${table} WHERE ${column} LIKE ${likewhat} LIMIT ${limit}`,function( err, row ){

        if(row && row.length){
            console.log("Case row found");
            returnvariable = true;

        }else{
            console.log("Case row not found");
            returnvariable = false;
        }
    });
    return returnvariable;
}

var test = (check_sql('*', 'country', 'Population', '3520000', '1'));
console.log(test);
//`SELECT * FROM finn_ads WHERE link_address LIKE "${data_text}" LIMIT 1;`;

con.end();
console.log("MySQL successfully ended");
0 Answers
Related