TSNev
TSNev

Reputation: 163

Can you customize a mysql_error duplicate error message?

I'm getting a "Duplicate entry 'blah' for key 'username'" error message, but I would LOVE for it to read "This username already exists".

Is that possible? If so, where and how would I go about changing that?

I'm using PHP, MySQL and phpmyadmin.

Sorry for my noobishness. Thanks in advance.

Upvotes: 0

Views: 3170

Answers (1)

Marc B
Marc B

Reputation: 360702

$result = mysql_query('... query that produces duplicate key error ...');
if ($result === FALSE) {
    if (mysql_errno() == 1022) {
        die("Username already exists");
    } else {
        die(mysql_error());
    }
}

The error codes are documented here: http://dev.mysql.com/doc/refman//5.5/en/error-messages-server.html and you can write your own custom error handler to output 'nicer' error messages if you so desire.

Upvotes: 3

Related Questions