Guest User

Untitled

a guest
Jan 10th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. npm install mysql
  2.  
  3. const mySQL = require('mysql');
  4.  
  5. const mySQLCon = mySQL.createConnection({
  6. host: "localhost",
  7. user: "username",
  8. password: "password",
  9. database: "databaseName"
  10. });
  11.  
  12. function connectTest() {
  13. mySQLCon.connect(function(err) {
  14. if(err){
  15. console.log(err.code);
  16. console.log(err.fatal);
  17. }
  18. });
  19. console.log(mySQLCon.state);
  20. sSQL = "SELECT sField FROM tblName WHERE iID = 1";
  21. mySQLCon.query(sSQL, function(err, rows, fields) {
  22. if(err){
  23. console.log("An error ocurred performing the query.");
  24. return;
  25. }
  26. if (rows.length > 0) {
  27. console.log(rows[0].sField);
  28. } else {
  29. console.log("No Records found!");
  30. }
  31. });
  32. mySQLCon.end(function(){
  33. // The connection has been closed
  34. });
  35. }
  36.  
  37. connectTest()
  38.  
  39. console.log(mySQLCon.state);
Add Comment
Please, Sign In to add comment