Advertisement
Guest User

Untitled

a guest
Feb 15th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. let mysql = require('mysql');
  2.  
  3. // -------------------
  4. // set up connection
  5. // -------------------
  6. let connectionMysql = mysql.createConnection({
  7. host: '127.0.0.1',
  8. user: 'root',
  9. password: 'MysqlPass', // your passsword (if exists)
  10. database: 'sys'} // your database schema name
  11. );
  12.  
  13. // ---------------------------------
  14. // connect to database from server
  15. // ---------------------------------
  16. connectionMysql.connect(function(err) {
  17. if (err) {
  18. console.error('error connecting: ', err.stack);
  19. return;
  20. }
  21. console.log('connected as id: ', connectionMysql.threadId);
  22. });
  23.  
  24. // ----------------------------
  25. // select all data from table
  26. // ----------------------------
  27. connectionMysql.query(
  28. 'SELECT * from Users', function(err, result, fields) {
  29. if (err) {
  30. throw err;
  31. }
  32. console.log('result: ', result);
  33. }
  34. );
  35.  
  36. // --------------------------
  37. // insert data into a table
  38. // --------------------------
  39. let post = {
  40. username: reqBody.username.toString(),
  41. email: reqBody.email.toString(),
  42. createdDate: dateFormat(new Date())};
  43.  
  44. connectionMysql.query('INSERT INTO Users SET ?', post, function(err, result) {
  45. if (err) {
  46. throw err;
  47. }
  48. SendMail.sendEmail(reqBody.email, reqBody.username, res);
  49. });
  50.  
  51. // -----------------------------------
  52. // update multiple data into a table
  53. // -----------------------------------
  54. connectionMysql.query('UPDATE Users SET password = ?, active = ? WHERE hashField = ?', [reqBody.password, '1', reqBody.hashed], function(err, result) {
  55. if (err) {
  56. throw err;
  57. }
  58. res.redirect('/create-team');
  59. console.log(JSON.stringify(result, null, 2));
  60. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement