下面给出了使用node.js从MySQL数据库访问和获取值的代码。该文件为app.js
:
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
database : 'dbname'
});
connection.connect();
connection.query('SELECT * from table1', function(err, rows) {
if (err) throw err;
//console.log('The solution is: ', rows);
var arr=rows
console.log(arr)
respone.
});
connection.end();
我得到的值存储在arr
中。如何在任何网页上访问这些值?
答案 0 :(得分:0)
这只是你的榜样。如何使用您的代码。
var http = require('http');
var mysqlquery = require('.\mysqlquery');
http.createServer(function (req, res) {
mysqlquery(err, data){
console.log(data); // your mssql data
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}
}).listen(3000, '127.0.0.1');
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
database : 'dbname'
});
module.exports = function(callback) {
connection.connect();
connection.query('SELECT * from table1', function(err, rows) {
if (err) throw err;
var arr=rows;
callback(null, arr);
connection.end();
});
}