almost 3 years ago

MySQL 簡單查詢範例

node.js

   var mysql = require('mysql');
   var connection = mysql.createConnection({
     host: '127.0.0.1',
     port: '3306',
     user: 'root',
     password: 'root',
     database: 'nodejs'
   });
  
  connection.connect();
  
  connection.query('SELECT * FROM `test`', function(err, rows, fields){
    if(err) throw err;
    for(var r in rows){
      console.log(rows[r].id);
    }
  });
 
 connection.end();

在 express 的 router 中這樣用:

routers/index.js

router.get('/', function(req, res){
    connection.query('SELECT * FROM `test`', function(err, rows, fields){
    res.render('index', {title: rows[0].id});
  });
});
← [Python] Requests + Google Finance API 常用 debug 工具筆記 →
 
comments powered by Disqus