Skip to content

Instantly share code, notes, and snippets.

@pepoviola
Created March 4, 2012 20:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pepoviola/1974743 to your computer and use it in GitHub Desktop.
Save pepoviola/1974743 to your computer and use it in GitHub Desktop.
node.js and mysql module
var Client = require('mysql').Client,
client = new Client();
client.user = 'user';
client.password = 'password';
client.host='127.0.0.1';
client.port='3306';
client.database='DB'
client.connect();
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
client.query("select * from table where campo1 > 1 limit 10;",
function select(err, results, fields) {
if (err) {
console.log("Error: " + err.message);
throw err;
}
console.log("Number of rows: "+results.length);
console.log(results);
res.write(results);
//loop over the result set
for (var i in results){
var result = results[i];
res.write(result.campo1+"\n");
}
res.end();
});
}).listen(1337, "0.0.0.0");
console.log('Server running ');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment