Is there a way that I can check for specific errors in MySQL with PHP? For example, I want to run a query that inserts 1000 last names into a database table, and want to be able to detect any errors and handle them.
有没有办法可以用PHP检查MySQL中的特定错误?例如,我想运行一个将1000个姓氏插入数据库表的查询,并希望能够检测到任何错误并处理它们。
I know that I can use mysql_error()
to check for errors, but I want to specifically check for a duplicate value
error only. Is there any way to do that?
我知道我可以使用mysql_error()来检查错误,但我想专门检查重复值错误。有没有办法做到这一点?
1 个解决方案
#1
3
You could compare the error message. There is also mysql_errno()
: http://www.php.net/manual/en/function.mysql-errno.php. You could find the exact error num for Duplicate Value from the MySQL spec for a better comparison.
您可以比较错误消息。还有mysql_errno():http://www.php.net/manual/en/function.mysql-errno.php。您可以从MySQL规范中找到Duplicate Value的确切错误num,以便更好地进行比较。
#1
3
You could compare the error message. There is also mysql_errno()
: http://www.php.net/manual/en/function.mysql-errno.php. You could find the exact error num for Duplicate Value from the MySQL spec for a better comparison.
您可以比较错误消息。还有mysql_errno():http://www.php.net/manual/en/function.mysql-errno.php。您可以从MySQL规范中找到Duplicate Value的确切错误num,以便更好地进行比较。