row(s) affected in mysql update with PHP

前端 未结 10 1847
臣服心动
臣服心动 2020-12-10 14:08

How do i find if my update is successful or not? i update using a where uniqueName=name so i should always only update 0 rows or 1. What is a good way to check if i updated

10条回答
  •  时光说笑
    2020-12-10 14:50

    So what if the user resubmits data that is already the same as the information in the DB? If memory serves that would return mysql_affected_rows() with a value of 0 because by definition no rows were updated in the process. Yet the query itself was successful.

    The workaround to this would be to either proof your content before insertion or using:

    $result = mysql_query($q);
    if( mysql_affected_rows() == 0 )
        $state = $result ? "Success" : "Fail";
    

    This way you get to check if the rows were updated and if not you can check if it was a malfunction or just repeat data.

提交回复
热议问题