Node.js 连接数据库

2021-12-08 08:38:07 浏览数 (1)

首先使用npm install mysql

代码语言:javascript复制
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();

0 人点赞