我更倾向于node.js而且我只知道javascript的一些基础知识,抱歉如果我出错了。
要在node.js中打印由sql查询检索的结果,我尝试使用
<p>Users are " + util.inspect(results) + ".</p>"
我也尝试过使用
var out = JSON.stringify(results);
<p> Users are " + out + ".</p>"
这两者的结果如下:
Users are [ { name: 'manohar' }, { name: 'bhai' } ]
我的疑问是如何打印出来像
Manohar
bhai
我的db.js代码是:
var http = require("http");
var port = 3000;
var serverUrl = "localhost";
var mysql = require('mysql');
var util = require('util');
var connection = mysql.createConnection({
host : 'localhost',
user : 'xxxxxxxx',
password: 'xxxxxxx',
database: "wst"
});
var server = http.createServer(function(req, res) {
connection.query('select * from welcome',function(err, results, fields){
var html = "<p> Users are " + util.inspect(results) + "</p> <br/>";
res.end(html);
});
});
console.log("Listening at " + serverUrl + ":" + port);
server.listen(port, serverUrl);
答案 0 :(得分:0)
我不知道节点,但请尝试:
...
var server = http.createServer(function(req, res) {
connection.query('select * from welcome',function(err, results, fields){
var html;
if (results.length > 0) {
html = "<p> Users are ";
for (var i in results) {
html += i.name + "<br />";
}
} else {
// no users found
}
html += "</p>";
util.puts(html);
res.end(html);
});
...
util.inspect用于对javascript对象进行字符串化。