簡體   English   中英

使用 Node.JS 查詢 MySQL 並在網頁中顯示結果

[英]Querying MySQL with Node.JS and display results in webpage

我的目標是使用 Node.JS 查詢 MySQL 以獲得更好的交互性。 我的查詢是一個帶有JOIN的簡單SELECT 我設法構建了一個在控制台中顯示結果的腳本,但是在網頁中顯示它時我有點卡住了。 這是我的代碼:

var mysql = require('mysql');

var mySqlClient = mysql.createConnection({
    host    : 'server',
    user    : 'login',
    password: 'pwd',
    database: 'db'
});

var selectQuery = 'SELECT fields FROM table t1\
                   INNER JOIN table t2\
                   ON t1.field = t2.field';
mySqlClient.query(
    selectQuery,
    function select(error, results, fields) {
        if(error) {
            console.log(error);
            mySqlClient.end();
            return;
        }

        if(results.length > 0) {
            console.log(results);
        } else {
            console.log('No data');
        }
        mySqlClient.end();
    });

將其包含在網頁中的最佳方法是什么? 我需要用 Node.JS 創建一個 httpServer 嗎?

非常感謝您的幫助!

編輯我想在使用 Zend Framework 設計的 PHP 應用程序中使用 Node.JS。 Node.JS 應用程序不會是我項目的唯一應用程序。

最簡單的方法是使用節點框架。 喜歡快遞

你會做類似的事情

/*** omitting generic code for clarity ***/
app.get('/yourpage',function(req,res){

    //On request of this page initiating sql query. Assumes that the object initialization is done above.
    mySqlClient.query(
    selectQuery,
    function select(error, results, fields) {
        if(error) {
            console.log(error);
            mySqlClient.end();
            //render the template with error to be alerted
            res.render('tempaltefile',{data:null,error:error});                
        }

        if(results.length > 0) {
            //console.log(results);
            //render the template with fetched data
            res.render('tempaltefile',{data:results,error:null});
        } else {
            //console.log('No data');
            //render the template with empty data alert
            res.render('tempaltefile',{data:null,error:"no data"});
        }
        mySqlClient.end();
    });

});

回復評論

請提及您在 ph 代碼旁邊使用它。 我認為您必須將節點代碼構建為 api 並在 php 端使用它。

我和你的情況一樣,自從 NodeJS 以來我無法連接到 MySQL Zend。 我的解決方案是使用:

最后,重新啟動計算機以應用新安裝。

  • 在您的 server.js 文件中,您可以使用以下代碼連接到您的 HostDB:

    var app = require('http').createServer(handler), mysql = require('mysql'), connectionsArray = [], connection = mysql.createConnection({ host: 'localhost', user: 'USERNAME MYSQL', password : 'PASSWORD MYSQL', 數據庫: 'NAME MYSQLDATABASE', 端口: 3306 });

app.post('/qr', function(req, res) {
  console.log("Check QR code.");
  let now = moment().format('MMMM Do YYYY, h:mm:ss a');
  let subnow = now.substr(0, 8);
  let subnowwild = subnow + "%";
  connection.query("select exists (select * from visit where timeIn like ?)", subnowwild, function(err, result) {
  if (err) throw err;
  console.log(result);
    if(result = 0) {
     res.redirect(307, '/savedata');
    }
    else {
     res.redirect(307, '/updatedata');
    }
  });
});

所以我想看看用 mysql 獲取數據時的響應是什么樣的,這個網頁出現了,但沒有顯示我正在尋找的答案。

你也可以使用res.send(result); 如果您希望查詢將數據作為 json 響應返回。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM