[英]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.我目前正在编写一个应用程序,我正在查询我的 MySQL 数据库表,它有三行。 I want to save each of the rows as a variable/string.我想将每一行保存为变量/字符串。
Eg SELECT * FROM orders LIMIT 1
where the response is例如SELECT * FROM orders LIMIT 1
其中响应为
[
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.如何从结果中提取 email 并将其设置为诸如“电子邮件”之类的变量以供将来在我的应用程序中使用。
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.当我尝试执行 result.email 时,我错过了数组 position。
Solution is解决方案是
email = result[0].email;
assuming I want the email.假设我想要 email。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.