2014-07-16 32 views
0

我已經在我的機器上安裝了WAMP服務器。我可以使用node-mysql模塊訪問在WAMP的MySQL上創建的MySQL數據庫嗎?如何在node.js中使用現有的wamp的MySQL數據庫?

其實,我嘗試這個代碼,它沒有錯誤,但無法提取數據庫(或表)運行:

var http = require('http'), 
    mysql = require("mysql"); 

var connection = mysql.createConnection({ 
    host: "localhost", 
    user: "root", 
    password: "", 
    database: "database_name" 
}); 

http.createServer(function (request, response) { 
    request.on('end', function() { 
     connection.query('SELECT * FROM table_name', function (error, rows, fields) {    
      console.log('The first field is: ', rows[0].field); 
     }); 
    }); 
}).listen(8001); 

console.log("running on localhost:8001"); 
+0

您不需要任何與'http = require('http')'有關的代碼來測試mysql連接。如果有的話,你也應該打印錯誤。將'console.log(error)'添加到您的查詢回調中。 –

+0

感謝您的幫助。 – nole

回答

0

嘗試的「結束」事件處理程序前加入request.resume();

在節點v0.10 +中,流開始時處於「暫停」狀態,允許您訪問特定大小的塊,或者您可以像舊流一樣使用它們,方法是附加一個'data'事件處理程序,不斷讀取。

調用request.resume();也將切換到舊的流模式,有效地丟棄請求數據(因爲沒有「數據」事件處理程序),讓你的「結束」事件處理程序將被調用。

相關問題