Home > Web Front-end > JS Tutorial > body text

What are the methods for nodeJs to connect to mysql?

php中世界最好的语言
Release: 2018-04-12 16:56:32
Original
1424 people have browsed it

This time I will bring you nodeJs connectionmysqlWhat are the methods, what are the notesfor connecting nodeJs to mysql, the following is a practical case, let's take a look.

mysql.js

//连接数据库
var mysql = require('mysql');
var pool = mysql.createPool({
  host: 'localhost',
  user: 'root',
  password: 'gfdfsdf34r34r',
  database:'caiji'
});
module.exports = function(sql, callback) {
  pool.getConnection(function(conn_err, conn) {
    if(conn_err) {
      callback(err,null,null);
    } else {
      conn.query(sql, function(query_err, rows, fields) {
        conn.release();
        callback(query_err, rows, fields);
      });
    }
  });
};
Copy after login

Call

var query = require("./mysql.js");
query(sql, function(err, rows, fields) {
  console.log(rows);
});
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to achieve the undo and redo effect in Immutable.js

easyui date and time box in IE How to deal with compatibility

The above is the detailed content of What are the methods for nodeJs to connect to mysql?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!