I'm using OO MySQLi to work with a register form. I made the username column unique
in my table and noticed that $stmt
will throw an error when the code tries to use the execute()
method, if a duplicate username is given.
I'd like to know how I can differentiate between other errors and a duplicate username error in the error detection? At the moment I'm essentially doing this:
$stmt->execute();
if($stmt->error)
die();
The problem is that if execute()
fails for another reason (instead of duplicate username), I'll most likely want to log it, but I don't want to log everytime a user tries to register with a taken username. The simplest (and probably not a very good) solution I came up with right away was to analyze the $stmt->error
message. If the problem is about duplicate usernames, the string will be "Duplicate entry 'value' for key 'column'"
, so I could just check if the strings first words are "Duplicate entry "
. But I guess that'd be prone to errors.
Any better suggestions?