var mysql = require(‘mysql’); var pool = mysql.createPool({ host: 'localhost’, user: 'root’, password: 'root’, database: 'bzbsystem’, port: 3306 });
pool.getConnection(function (err, conn) { if (err) console.log("POOL ==> " + err); var selectSQL = 'select count(*) as minzuCount from minzu’; conn.query(selectSQL,function(err,rows){ if (err) console.log(err); row = rows[0]; global.minzuCount = row.minzuCount;
console.log('111'+global.minzuCount);
//这里输出正确 }); var selectSQL = 'select * from minzu limit 10’; conn.query(selectSQL,function(err,rows){ if (err) console.log(err); global.minzuList = rows; });
conn.release();
console.log('qqq’+global.minzuCount); //这里得不到值,程序第一次运行是有结果的,试过在函数外定义变量,也得不到值,感觉是回调时使用的问题,求帮忙理解node的回调,以及mysql如何操作比较好? res.render('list’,{totalCount:global.minzuCount,list:global.minzuList})