I'm doing an update query with PDO. I would like to figure out if my update query did not change anything in the database, since:
rowCount()
in such a case returns 0
.rowCount()
in such cases also returns 0
.Am I forced to precede my UPDATE by a SELECT statement, to figure out if the record I'm trying to update does in fact exist? Or is there another common practice for this sort of thing.
I've been perusing through the documentation, but cannot find a conclusive answer: http://php.net/manual/en/pdostatement.rowcount.php
I've come across this StackOverflow answer, that suggests that rowCount() might return NULL in some scenario's, but I don't think it's apliccable to my scenario: see Why does PDO rowCount() return 0 after UPDATE a table without modifying the existing data?
From the comments in this question:
If the data hasn't been modified, the rowCount will be zero. If the data was modified, the rowCount will be one or higher. If there was an error, rowCount will be null or false or something non-zero.
UPDATE I've found another question that gives an example of the proposition in the comments below: Getting the insert and update ID with PDO
UPDATE2
Another question proposes another solution, via PDO::MYSQL_ATTR_FOUND_ROWS
PDO - check if row was updated?
You could add conditionals to your 'where' clause so such as " and ColumnToUpdate <> 'NewValue'"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With