Hey guys,
I need your help with a problem I’ve been able to walk around so far.
Namely, I want to make a mysql query in a node.js web socket:
connection.query("SELECT * FROM DB WHERE id= ?;", [id], function(error, results, fields) {
if (error) throw error;
// Code here...
})
In the variable results I can now use the data in the form of a JSON object. That’s not a problem.
So far, I have done this so that when I wanted to make several queries, I have nested into each other. Like this:
connection.query("SELECT * FROM DB WHERE id= ?;", [id], function(error, results, fields) {
if (error) throw error;
connection.query("SELECT * FROM DB WHERE id= ?;", [id], function(error, results, fields) {
if (error) throw error;
// Code here...
})
})
But now I need that some queries are only carried out if certain conditions are met. As a result, the queries can no longer be logically nested.
That’s why I need something like this:
if(a == b) {
connection.query("SELECT * FROM DB WHERE id= ?;", [id], function(error, results1, fields) {
if (error) throw error;
// Code here...
})
}
if(c == b) {
connection.query("SELECT * FROM DB WHERE id= ?;", [id], function(error, results2, fields) {
if (error) throw error;
// Code here...
})
}
And after both if loops I can logically no longer access the values results1 and results2.
How can I do that? Do you need more information.
I hope for some answers.
Yours, Ben