I am querying a mySQL database from the context of Node.js server code. Generally speaking, things are working fine. However, I can't figure out a way to tell whether an UPDATE statement does not update any rows. For instance, if I have code such as:
connection.query("UPDATE table SET columnA = valA WHERE columnB = valB;",
function(err, rows, fields) {
..some code..
}
As far as I can tell, none of the callbacks (err, rows, fields) will provide me with any information informing me that zero rows were affected. This is important information to me. How can I resolve this? Thanks!
I am querying a mySQL database from the context of Node.js server code. Generally speaking, things are working fine. However, I can't figure out a way to tell whether an UPDATE statement does not update any rows. For instance, if I have code such as:
connection.query("UPDATE table SET columnA = valA WHERE columnB = valB;",
function(err, rows, fields) {
..some code..
}
As far as I can tell, none of the callbacks (err, rows, fields) will provide me with any information informing me that zero rows were affected. This is important information to me. How can I resolve this? Thanks!
Share Improve this question edited Dec 11, 2015 at 15:54 Sterling Archer 22.4k19 gold badges85 silver badges121 bronze badges asked Dec 11, 2015 at 15:50 Tim ClotworthyTim Clotworthy 1892 silver badges6 bronze badges1 Answer
Reset to default 8I assume you're using node-mysql. The count of changed rows is in result.changedRows
variable.
Try:
connection.query('UPDATE table SET ...', function (err, result) {
if (err) throw err;
console.log('changed ' + result.changedRows + ' rows');
});
You can also get result.affectedRows
.
Further info is within the docs.