Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let mysql = require('mysql');
- // -------------------
- // set up connection
- // -------------------
- let connectionMysql = mysql.createConnection({
- host: '127.0.0.1',
- user: 'root',
- password: 'MysqlPass', // your passsword (if exists)
- database: 'sys'} // your database schema name
- );
- // ---------------------------------
- // connect to database from server
- // ---------------------------------
- connectionMysql.connect(function(err) {
- if (err) {
- console.error('error connecting: ', err.stack);
- return;
- }
- console.log('connected as id: ', connectionMysql.threadId);
- });
- // ----------------------------
- // select all data from table
- // ----------------------------
- connectionMysql.query(
- 'SELECT * from Users', function(err, result, fields) {
- if (err) {
- throw err;
- }
- console.log('result: ', result);
- }
- );
- // --------------------------
- // insert data into a table
- // --------------------------
- let post = {
- username: reqBody.username.toString(),
- email: reqBody.email.toString(),
- createdDate: dateFormat(new Date())};
- connectionMysql.query('INSERT INTO Users SET ?', post, function(err, result) {
- if (err) {
- throw err;
- }
- SendMail.sendEmail(reqBody.email, reqBody.username, res);
- });
- // -----------------------------------
- // update multiple data into a table
- // -----------------------------------
- connectionMysql.query('UPDATE Users SET password = ?, active = ? WHERE hashField = ?', [reqBody.password, '1', reqBody.hashed], function(err, result) {
- if (err) {
- throw err;
- }
- res.redirect('/create-team');
- console.log(JSON.stringify(result, null, 2));
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement