How to know if a MySQL UPDATE query fails because information being supplied matches data already in the database?

前端 未结 2 1251
天涯浪人
天涯浪人 2021-01-19 06:02

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 t

2条回答
  •  失恋的感觉
    2021-01-19 06:45

    Since the query is successful MySQL should not return any error number (if it does it'll be 0). In that case, you can check if the number of affected rows is 0 and the error number returned is 0.

    If you're coding in PHP, you can do something like this:

    // ... mysql_query() with the UPDATE query
    
    if (mysql_affected_rows() == 0 && mysql_errno() == 0)
    {
        // Data was not changed
    }
    

提交回复
热议问题