gpt4 book ai didi

javascript - 如何在 `node-mysql` 查询中获取结果的值

转载 作者:行者123 更新时间:2023-11-29 03:26:37 25 4
gpt4 key购买 nike

我正在对一个 node.js 应用程序进行加权,我从 mysql 查询中得到的结果是,

[ RowDataPacket { name: 'ubuntu' } ]

(Ubuntu 是该行中唯一的东西)

我想做的是缩短我的变量“results”,例如使其等于 ubuntu,或者只是“”之间的所有内容,我是 JS 新手。我正在使用查询 sql 数据库的标准方式, 这样做是这样的:

   var mysql      = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root', //just using root for my personal testing.
password : 'root',
database : 'Sonic'
});

connection.connect();

var connect = connection.query( 'SELECT name FROM Sonic_url',
function(err, fields, results, rows) {
// if (results === input) {
var sqldata = results.substring(1, 4);
console.log(results);
if (err) throw err;


// console.log('I belive we have found what you are after, is: ' + input + ' ' + 'what you are after?');
//}
});

我希望能够使用变量输入和来自 mysql 查询的变量执行基本 IF,这样无论是否找到结果,我都可以打印到屏幕上。

最佳答案

mysql 的正确签名查询是:

connection.query(query, function (error, results, fields) {
// error will be an Error if one occurred during the query
// results will contain the results of the query
// fields will contain information about the returned results fields (if any)
});

您想要记录name 的值。如果您的查询产生一个结果,您可以从 rows 数组中的第一项访问该值:

connection.query('SELECT name FROM Sonic_url', function(err, rows, fields) {
console.log(rows[0].name);
});

关于javascript - 如何在 `node-mysql` 查询中获取结果的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35184129/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com