parse mysql error msg - is it the right way?
In my php/MySQL code I insert new record into a table (InnoDB) which has m开发者_高级运维any UNIQUE column keys. If insertion fails (with ER_DUP_ENTRY) I need to know which column's value wasn't unique.
I was told that to achieve this, you must extract value of %d from error message you get: "Duplicate entry '%s' for key %d".
This should work but I don't like the solution. Parsing/regex matching some string gives unnecessary complexity to simple task I always try to avoid. Now the question: is this the right way to go? Isn't there any better solution?
It is probably possible to not get just the error message but also the corresponding error code too. See for example the results for PHP functions containing errno
.
精彩评论