Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. /*
  2. NODE and MySQL Example
  3. */
  4. var mysql = require('mysql');
  5.  
  6. const readline = require('readline').createInterface({
  7. input: process.stdin,
  8. output: process.stdout
  9. })
  10.  
  11. const con = mysql.createConnection({
  12. host: "localhost",
  13. user: "root",
  14. password: "helloworld",
  15. database: "school"
  16. });
  17.  
  18. con.connect( (err) => {
  19. if (err) throw err;
  20.  
  21. let sql = "SELECT * FROM student";
  22.  
  23. con.query(sql, (err, result) => {
  24. if (err) throw err;
  25. let data = JSON.parse(JSON.stringify(result));
  26.  
  27. for (const row of data) {
  28. console.log(row.name);
  29. }
  30.  
  31. readline.question("Type name of student: ", (name) => {
  32. readline.close();
  33.  
  34. sql = `SELECT email FROM student WHERE name = "${name}"`;
  35.  
  36. con.query(sql, (err, result) => {
  37.  
  38. let data = JSON.parse(JSON.stringify(result));
  39.  
  40. for (const row of data) {
  41. console.log(row.email);
  42. }
  43.  
  44. con.end();
  45. process.exit();
  46.  
  47. });
  48. });
  49. });
  50. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement