1

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.

Anurag Srivastava
  • 14,077
  • 4
  • 33
  • 43
Lube
  • 318
  • 1
  • 12
  • 1
    You could try to get the error number like [here](https://stackoverflow.com/a/12227643/7867822) and return a custom message based on that – Anurag Srivastava Feb 17 '22 at 15:17
  • This would work if I want to create generic error, for example : **Error, some of the fields are already linked**, but I am wondering how to get from this string, or sql error which field exactly is duplicate? – Lube Feb 17 '22 at 15:21

0 Answers0