【问题标题】:NodeJS and Mysql Query FetchNodeJS 和 Mysql 查询获取
【发布时间】:2016-11-27 14:45:11
【问题描述】:

嘿,我是 NodeJS 的新手,我想知道 .. 如何在此处获取 PHP 之类的 mysql 查询

$query = mysql_query("SELECT * FROM accounts");
while($fetch = mysql_fetch_array($query)) {
echo $fetch['Username'];
}

它在 NodeJS 中的样子?

还有一件事

我正在使用 ExpressJS 和我的 users.js

var express = require('express');
var router = express.Router();
var db = require('../database');
/* GET users page. */
router.get('/' , function(req, res, next) {
     db.query('SELECT * FROM accounts', function(err, rows) {
         // Fields > Return Table Fields ( Name , type , length .... )
         // rows > return Rows Data

               db.query("select * from comments where user = ?",
                     [rows[0].Username],
                     function(err, rowx) {

                         console.log('User Comments : '+rowx);
                         console.log('Usernames : '+rows.Username);
                     });
 if (err) throw err;
             numRows = rows.length;
             res.render('users', {
                 title: 'Users Table',
                 rowCount: numRows,
                 rowsData: rows
             });

         }); // END OF Query
     });

module.exports = router;

如何使用帐户表中的用户名获取评论表中的数据?

【问题讨论】:

    标签: javascript mysql node.js database express


    【解决方案1】:

    你应该使用 async 包来使用 nodejs 像 php 你在 nodejs 中的第一个代码块可以是这样的:

    var async=require('async');
    async.auto(
    {
       getAccounts:function(cb,results){
          db.query('SELECT * FROM accounts', function(err, rows) {
               cb(null,rows)
         })
       },
       showRows:['getAccounts',function(cb,results){
           var accounts=results.getAccounts;
           for(var account in accounts){
              console.log(accounts[accont])
           }
      }]
       },
       function(err,allResult)
       {
        }
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-22
      • 1970-01-01
      • 2021-11-02
      • 2021-12-22
      • 2015-10-24
      • 1970-01-01
      • 2015-07-26
      • 2018-09-30
      相关资源
      最近更新 更多