javascript - How to check if row exists in mysql, in Node js -
i check in record exists in mysql, working in node js. code is:
//some code before classes (var m = 0; m <= (classes.length-1); m++) { var myurl = classes[m]; (function(myurl){ var connection = mysql.createconnection({ host: 'localhost', user: 'root', password: 'password', database: 'mydatabase', }); connection.connect(); //how write query correctly? var query = connection.query('select * mydatabase urllink=? limit 1', [myurl], function(err, rows, fields) { if (err) throw err; sqlnames = rows; lognames(); //if urllink not present in mydatabase, want proceed with: request(myurl, function(err, resp, body) { // ... } }); function lognames() { console.log(sqlnames); } connection.end(); }; })(myurl);
so, want check if record exists in database, , if not exist want process request, , add database. error:
connection not defined
the reason connection undefined because creating in javascript closure. when initialize variable inside function, scope prevents being accessed outside function.
here sample code:
var myurl = classes[m]; var connection = mysql.createconnection({ // notice how not in function? host : 'localhost', user : 'root', password : 'password', database : 'mydatabase', }); connection.connect(); // before.
without knowing database schema, cannot tell if proper query, looks correct doing.
Comments
Post a Comment