views:

48

answers:

2

Hi!

I tried searching for a solution in this website but I can't seem to find any.

I needed to find a way to know which field in my table is causing the constraint error.. In my table, say members table two fields (username,email) are unique keys and defined as unique keys apart from my primary key (memberid). When i add data to this table for a similar user and/or email address this error occurs:

"duplicate entry '[email protected]' for key2"

i want to tell the user which field is causing duplicate entry so as to say "that email is already registered"

I cannot seem to find a mysql function to loop through error keys upon insert...

Thanks in advance!

+2  A: 

On my sites, I actually check the unique fields myself with Select 1 from tab where field = 'value' limit 1; just so that I can give nice error messages instead of encountering an exception coming back from my insert statement.

Don
A: 

i guess naming constraint fields is not yet a feature in mysql. hope they consider this as this is very useful and saves a lot of code times and lines.

jan