PHP - detect mysql update/insertion failure due to violated unique constraint

前端 未结 7 1261
无人及你
无人及你 2020-11-30 11:10

This is kind of similar to this question:

PHP MySQL INSERT fails due to unique constraint

but I have a different twist. Let\'s say I have a table with only

相关标签:
7条回答
  • 2020-11-30 12:11

    From PHP Documentation on the function mysql_errno:

    Returns the error number from the last MySQL function, 
    or 0 (zero) if no error occurred. 
    

    Also, from MySQL Documentation on Constraint Violation Errors, error code 893 corresponds to:

    Constraint violation e.g. duplicate value in unique index
    

    So, we can then write something like this to do the work:

    if (!$result) {
        $error_code = mysql_errno();
        if ($error_code == 893) {
            // Duplicate Key
        } else {
            // Some other error.
        }
    }
    
    0 讨论(0)
提交回复
热议问题