I'm trying to use a SELECT query on my SQL database using node.js, the SELECT query below should work but doesn't. This is weird because I think this should work but it doesn't even enter the client.query function. I also copied my INSERT query which is actually working to show you my connection to the database works fine.
What am I doing wrong in the SELECT query?
CreateConnection
var mysql = require('mysql');
var client = mysql.createConnection({
host: '***',
user: '***',
password: '***',
database: '***'
});
SELECT Query (Not working)
function query(sql){
var returnvalue = "tempVal";
client.connect();
client.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
returnvalue = "doens't even enter function";
if (err) throw err;
returnvalue = rows[0].solution;
});
client.end();
return returnvalue;
}
INSERT Query (Is working)
function query(sql){
client.connect();
client.query('INSERT INTO users (username) VALUES("foo")');
client.end();
}