I have a query where the user types a block of text in a textarea field. They are able to save this information in a database. The problem is that if they have not changed the information or if the information matches the data already in the database, I recieve a ‘0’ for affected rows. Usually I display an error that says the query failed when there are no affected rows. How would I ‘know’ that the 0 affected rows is because the data already exists, so that I can display a more specific error?
Share
Another reason you’d get 0 affected rows is if the
UPDATEstatement matches no rows. For instance:Gives 0 affected rows if no row exists with
id = 1234. This is not an error either, it’s just anUPDATEthat happened to match no rows.The way to detect this case is to use
SELECTto verify that there is such a row. If you can confirm the row exists, but theUPDATEsaid it affected 0 rows, then you know that the values you tried to change were in fact the rows already in the database.But the distinction may not be important. You could report an error if
mysql_error()says there is one, as @BoltClock suggests.*If there is no error you could just report “no change” to the user.