Reputation: 524
I've had this problem for a while now and I can't seem to figure it out, I've looked around on Stackoverflow but could only find either PHP or C# code matching my problem. I'm trying to check if a row exist, if it doesn't then send a message that it does not, if it does send a message that it does.
Here's my code,
query('SELECT * FROM `cases` WHERE `case`='+pool.escape(String(getCase)), function(err, row) {
if(err) {
logger.error('Error in DB');
logger.debug(err);
return;
}else{
console.log('Case row was found!');
}
});
Also, getCase is a variable that I can change, but even when it's not in the database it seems to return Case row was found!
Upvotes: 0
Views: 15550
Reputation: 2559
The accepted answer does not work for me somehow. I found another solution here
Excerpt from the article:
You have a table address and want to check if there’s already a row with address_id = 100 existing. You can use COUNT to check:
SELECT COUNT(*) FROM address WHERE address_id = 100;
But if you are looking for a faster query with a yes/no answer:
SELECT EXISTS(SELECT 1 FROM address WHERE address_id = 100);
And I wanna add we also can use LIMIT
:
SELECT * FROM address WHERE address_id = 100 LIMIT 1;
Upvotes: 0
Reputation: 40892
It is returning Case row was found!
because you are only telling the callback to stop if an error occurred. You need to extend the else
statement to check if any data is present in the row
variable like this:
query('SELECT * FROM `cases` WHERE `case`='+pool.escape(String(getCase)), function(err, row) {
if(err) {
logger.error('Error in DB');
logger.debug(err);
return;
} else {
if (row && row.length ) {
console.log('Case row was found!');
// do something with your row variable
} else {
console.log('No case row was found :( !');
}
}
});
Upvotes: 3