Node.js ব্যাবহার করে আমি কি MySQL থেকে ডাটা retrieve করতে পারবো? পারলে তার কি বাস্তব উদাহরণ দেয়া যায় কি?

asked 21 Jan '15, 15:27

Minhaj%20Hasan's gravatar image

Minhaj Hasan
59421137


connection.query is asynchronous, so you'll have to use a callback function to handle the result of the query.

Here's your code slightly modified:

API route:

app.post('/getJson', function (req, res) {

fetchData(req.body.selectpicker, function(jsonata) {
    console.log('jsonData' + jsonData);
    res.render('result.ejs', {
        html: '<p>' + jsonData + '</p>'
    });
});

});

The fetchdata function:

function fetchData(tableName, done)

{

var mysql      = require('mysql');

var connection = mysql.createConnection({

    host : 'xxxx',

    user : 'xxxx',

    password : 'xxxx!',

    database : 'xxxxx',

    port : 'xxxx'


});


connection.connect(function(err) {

    console.log('error' + err);

    // connected! (unless `err` is set)

});

var query = "SELECT * FROM " + tableName;


connection.query(query, function(err, rows, fields)

{

    if (err) throw err;

    console.log('The rows are: ', rows);

    done(rows);

});


connection.end();

}

permanent link

answered 21 Jan '15, 15:32

ishahriyar's gravatar image

ishahriyar
9519

edited 21 Jan '15, 15:33

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×31
×15
×12
×6

question asked: 21 Jan '15, 15:27

question was seen: 1,753 times

last updated: 21 Jan '15, 15:33