Node.js 连接数据库

首先使用npm install mysql

var mysql = require('mysql');
var conn = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '123456',
    database: 'mysql',
    port: 3306
});
conn.connect();
conn.query(
    'select * from user',
    function (err, res, fields) {
        if (err) throw err;
        for (
            var i = 0;
            i < res.length;
            i++
        ) {
            var obj = res[i];
            console.log(obj);
        }

    }
);
conn.end();

 

1 thought on “Node.js 连接数据库

Leave a Comment

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据