Can you customize a mysql_error duplicate error message?

浪子不回头ぞ 提交于 2019-12-13 03:49:08

问题


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.


回答1:


$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.



来源:https://stackoverflow.com/questions/10202818/can-you-customize-a-mysql-error-duplicate-error-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!