I have https://www.npmjs.org/package/mysql module.
They show examples how to use it when multiple rows are expected, but I couldn't find one example showing how to fetch a single result into a variable.
SELECT name FROM users WHERE id=1 LIMIT 1
how do I fetch this into sqlValues
variable ?
In the callback function of the .query
method, the second parameter contains an array containing the rows returned from your query. No matter how many are returned, these can be indexed just as an array.
Thus, in the callback function to get the single expected result, you can use rows[0]
.
In your specific instance, to assign the returned name
field to the sqlValues
variable, you can use a simple assignment: sqlValues = rows[0].name