Before Entering data into a database, I just want to check that the database doesn’t have the same username in the database already.
I have the username in SQL set as a key, so it can’t be duplicated on that end, but I am looking at finding a more user-friendly error message then “KEY already exists”.
Is there are simple way to check if the variable value already exists in a row?
Either preform a check before attempting the insert, or catch the exception and display it in a more clean and user-friendly way in your application.
Edit: Take a look at this tutorial on PHP exception handling. You probably want to wrap your query execution in a try-catch block, like so:
Except, instead of catching a general type
Exception, you’ll want to figure out what sort of exception you’re actually getting (something likeMySQLDuplicateKeyException, or whatever it may be – echo the exception you get when testing, and use that). This way, you won’t display an error informing the user of an existing username, if in fact, there is another problem (like a DB connection error, for instance).Good luck!