Node code to connect to MySQL
var http = require("http");
var url = require("url");
function onRequest(request, response) {
var mysql = require('mysql');
var TEST_DATABASE = 'testdatabasename';
var TEST_TABLE = 'testtablename';
var result;
var client = mysql.createClient({
user: 'username',
password: 'password',
});
var result;
client.query('USE '+TEST_DATABASE);
var qry='SELECT * FROM '+TEST_TABLE+ '
client.query( qry, function selectCb(err, results, fields) {
if (err) {
console.log("error");
}
else
{
console.log(results.length);
if(results.length>0)
{
console.log("yes result found");
}
else
{
console.log("Sorry result not found");
res.redirect('http://nodesamples.blogspot.in');
}
}
client.end();
}
);
} http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
No comments:
Post a Comment