How do I know if a MySQL UPDATE query has completed because the information provided matches the data already in the database?

I have a query when a user enters a block of text in the textarea field. They can store 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 get "0" for the affected rows. Usually I show an error saying that the request failed when there are no affected rows. How would I “know” that the rows affected by 0 are related to the fact that the data already exists, so that I can show a more specific error?

+3
source share
2 answers

, 0 , - , UPDATE . :

UPDATE MyTable SET field = 'content' WHERE id = 1234;

0 , id = 1234. , UPDATE, .

SELECT . , , UPDATE , 0 , , , , .

SELECT COUNT(*) FROM MyTable WHERE id = 1234;

. , mysql_error() , , @BoltClock. * , " ".

* : , mysql_error(). , - .

+3

, MySQL ( 0). , 0 , 0.

PHP, - :

// ... mysql_query() with the UPDATE query

if (mysql_affected_rows() == 0 && mysql_errno() == 0)
{
    // Data was not changed
}
+2

Source: https://habr.com/ru/post/1744629/


All Articles