'How to parse Error Code: 1062 MySQL duplicate error into PHP custom error message?

When I enter duplicate field I get php error Error Code: 1062. Duplicate entry 'someValue' for key 'someField'

I wonder, how can I modify or parse in Php this error to something like The someField you are trying to insert is linked to another user someField can be different - phone number, bank account, email. Here is my code that returns error if it exist.

if (!$connection->getConnection()->query($sql) ) {
    return $connection->getConnection()->error;
};

I found same question, but it is for Go language.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source