admin管理员组

文章数量:1401509

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!

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!

Share Improve this question edited Apr 3, 2017 at 21:05 Martijn Ebbens asked Apr 3, 2017 at 20:40 Martijn EbbensMartijn Ebbens 5242 gold badges5 silver badges16 bronze badges 2
  • What database engine are you using? And what library/module are you using for the query() function? – sadmicrowave Commented Apr 3, 2017 at 21:09
  • @sadmicrowave sorry for not specifying, I use mysql – Martijn Ebbens Commented Apr 3, 2017 at 21:13
Add a ment  | 

2 Answers 2

Reset to default 3

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 :( !');
        }
    }
});

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; 

本文标签: javascriptCheck if ROW already exists in Database using query gt NodeJSStack Overflow