PDA

View Full Version : Detect Duplicate Entry & Return Code To PHP When Inserting To MySQL?


Sussex_Chris
02-17-2010, 01:48 PM
I have a column with a primary & unique key.

If I turn on or die(mysql_error()) I get the following error:
Duplicate entry '1571827683' for key 'PRIMARY'

Without the or die error it simply doesn't insert but is there a way for PHP to detect how many ID's were actually inserted by returning error codes?

abduraooft
02-17-2010, 02:13 PM
Without the or die error it simply doesn't insert but is there a way for PHP to detect how many ID's were actually inserted by returning error codes? Yes, check for mysql_errno() (http://php.net/mysql_errno) after insert.
(The error code for duplicate key is 1062, see http://dev.mysql.com/doc/refman/5.1/en/error-messages-server.html#error_er_dup_entry)

Sussex_Chris
02-17-2010, 03:04 PM
Yes, check for mysql_errno() (http://php.net/mysql_errno) after insert.
(The error code for duplicate key is 1062, see http://dev.mysql.com/doc/refman/5.1/en/error-messages-server.html#error_er_dup_entry)
Thanks a lot, this was perfect for what I needed :)