How do I save a MySQL query result into variables based on the row

Viewed 16

I'm currently writing an app where I am querying my MySQL database table that has three rows. I want to save each of the rows as a variable/string.

E.g SELECT * FROM orders LIMIT 1 where the response is

[
  RowDataPacket {
    orderid: 1,
    email: 'isadg232323fskdjghfjkhbdfkjhsdf@gmail.com',
    token: 'ABCDEF4TZX1A9G7Z'
  }
]

How do I extract the email from the result and set it as a variable such as "email" for future use in my app.

My current code:

var mysql = require('mysql');

var con = mysql.createConnection({
    host: "123.456.123.456",
    user: "user",
    password: "pass",
    database: "db"
  });
  
  con.connect(function(err) {
    if (err) throw err;
    con.query("SELECT * FROM orders LIMIT 1", function (err, result, fields) {
      if (err) throw err;
      console.log(result);
      con.end();
    });
  });
1 Answers

The answer was very simple. Thanks Asad! I was missing the array position when I tried to do result.email.

Solution is

email = result[0].email; assuming I want the email.

Related