Fresher
Fresher

Reputation: 892

unable to connect to database in nodejs?

I am reading this and tried to connect to MYSQL database using node js.Here is my program

var mysql =  require('mysql');
var connection =  mysql.createConnection({
host : "localhost",
    User : "root",
password: ""
 });
connection.connect();
connection.query("use test");
var strQuery = "select * from chat";    

connection.query( strQuery, function(err, rows){
if(err) {
    throw err;
}else{
    console.log( rows );
}
 })
  connection.destroy( );

I an not getting any error but also unable to get any output

Upvotes: 1

Views: 786

Answers (1)

Denys Séguret
Denys Séguret

Reputation: 382464

In order to have the program wait for your queries to be executed, replace

connection.destroy();

with

connection.end();

Upvotes: 2

Related Questions