updating records with prepared statements, checking if update worked

后端 未结 4 1811
一个人的身影
一个人的身影 2021-01-14 02:16

I have a query that updates a record on my database, it works fine but i wanted to know how to check if the update has happened so i can return true and display the right me

4条回答
  •  无人及你
    2021-01-14 02:55

    A few years late, but perhaps this could help someone..

    As others have mentioned already you can use affected_rows to check if the UPDATE query in a PREPARED STATEMENT has indeed updated any record. However, do note that if the submitted data is the same as the record in the database 'affected_rows' will return a zero (0).

    A workaround of mine is creating a column for TIMESTAMP with ON UPDATE CURRENT_TIMESTAMP. Then every time I run the query, I'll also pass on a NULL value to the column tracking the time - that way forcing the row to UPDATE every time the query is executed. Then all you do is check for affected_rows.

提交回复
热议问题