简体   繁体   中英

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

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.

Eg 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();
    });
  });

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM