Node.js MySQL Select From

Node.js MySQL Select From

MySQL package is used to get data from table in mysql using node js.

SELECT FROM mysql statement is used to fetch data from table in mysql database.

Example : 

const mysql = require('mysql');

 

const db = mysql.createConnection({

     host: "localhost",
     user: "root",
     password: "*******",
     database : "probdb"


  });

// connect to database

db.connect((err) => {

    if (err) {

        throw err;

    }else{

     console.log('Connected to database!');

     db.query("SELECT * FROM employee", function (err, result) {
          if (err) throw err;
              console.log(result);
          }); 
    }

});

Write above code in server.js file and run this program as below.

D:\nodejs\website> node server.js  

It gives output as below.

 [
  { id: 1, emp_name: 'manoj', emp_address: 'aryatechno,india'},
  { id: 2, emp_name: 'john roy', emp_address: 'stamford,usa'},
  
]

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

45897