Mysql Error Duplicate
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us mysql error 1062 duplicate entry for key primary Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community
Error Code 1062 Duplicate Entry For Key Primary
Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Handle error duplicate entry for key 'primary' mysql for duplicate entries - PHP MySQL up vote 20 down vote favorite 2 I have a PHP form which enters data into my MySQL database. My primary key is one of the user-entered values. When the user enters a value that already exists
Duplicate Entry '1' For Key 'primary' Mysql
in the table, the MySQL error "Duplicate entry 'entered value' for key 1" is returned. Instead of that error, I would like to alert the user that they need to enter a different value. Just an echoed message or something. I guess my question comes down to: how to turn a specific MySQL error into a PHP message Thanks edit: nickf's answer below is nice, but is there any way to discern between specific errors? php mysql error-handling share|improve this question edited Jun 30 '10 mysql replication duplicate entry for key primary at 6:39 asked Jun 30 '10 at 6:09 RobHardgood 103116 add a comment| 3 Answers 3 active oldest votes up vote 31 down vote accepted To check for this specific error, you need to find the error code. It is 1062 for duplicate key. Then use the result from errno() to compare with: mysql_query('INSERT INTO ...'); if (mysql_errno() == 1062) { print 'no way!'; } A note on programming style You should always seek to avoid the use of magic numbers (I know, I was the one to introduce it in this answer). Instead, you could assign the known error code (1062) to a constant (e.g. MYSQL_CODE_DUPLICATE_KEY). This will make your code easier to maintain as the condition in the if statement is still readable in a few months when the meaning of 1062 has faded from memory :) share|improve this answer edited Jun 30 '10 at 7:14 answered Jun 30 '10 at 6:47 jensgram 22.8k25675 1 Yeah, that's a good point. This didn't work though... are you sure 1022 is the right error code? I'll start looking too. Thanks –RobHardgood Jun 30 '10 at 7:10 Ah, it's 1062, cf. dev.mysql.com/doc/refman/5.1/en/… . Will edit answer. –jensgram Jun 30 '10 at 7:12 Yeah, I just found that too :D I plugged it in and it worked perfectly. Thanks jensgram, just the solution I was looking for! –RobHardgood Jun 30 '10 at 7:13 @RobHardgood You're welcome. –jensgram Jun 30 '10 at 7:15 @jensgram: +1 thanks for very useful
Connectors More MySQL.com Downloads Developer Zone Section Menu: Documentation Home MySQL 5.7 Reference Manual Preface and Legal Notices General Information Installing and Upgrading MySQL Using MySQL as a Document
Mysql Duplicate Entry For Key
Store Tutorial MySQL Programs MySQL Server Administration Security Backup and Recovery Optimization
Mysql Replication Error 1062 Duplicate Entry
Language Structure Globalization Data Types Functions and Operators SQL Statement Syntax The InnoDB Storage Engine Alternative Storage Engines mysql duplicate entry error High Availability and Scalability Replication MySQL Cluster NDB 7.5 Partitioning Stored Programs and Views INFORMATION_SCHEMA Tables MySQL Performance Schema MySQL sys Schema Connectors and APIs Extending MySQL MySQL Enterprise Edition http://stackoverflow.com/questions/3146838/handle-error-for-duplicate-entries-php-mysql MySQL Workbench MySQL 5.7 Frequently Asked Questions Errors, Error Codes, and Common Problems Sources of Error Information Types of Error Values Server Error Codes and Messages Client Error Codes and Messages Problems and Common Errors How to Determine What Is Causing a Problem Common Errors When Using MySQL Programs Access denied Can't connect to [local] MySQL server Lost connection to http://dev.mysql.com/doc/en/error-messages-server.html MySQL server Client does not support authentication protocol Password Fails When Entered Interactively Host 'host_name' is blocked Too many connections Out of memory MySQL server has gone away Packet Too Large Communication Errors and Aborted Connections The table is full Can't create/write to file Commands out of sync Ignoring user Table 'tbl_name' doesn't exist Can't initialize character set File Not Found and Similar Errors Table-Corruption Issues Administration-Related Issues Problems with File Permissions How to Reset the Root Password What to Do If MySQL Keeps Crashing How MySQL Handles a Full Disk Where MySQL Stores Temporary Files How to Protect or Change the MySQL Unix Socket File Time Zone Problems Query-Related Issues Case Sensitivity in String Searches Problems Using DATE Columns Problems with NULL Values Problems with Column Aliases Rollback Failure for Nontransactional Tables Deleting Rows from Related Tables Solving Problems with No Matching Rows Problems with Floating-Point Values Optimizer-Related Issues Table Definition-Related Issues Problems with ALTER TABLE TEMPORARY Table Problems Known Issues in MySQL Restrictions and Limits Indexes MySQL Glossary Related Documentation MySQL 5.7 Release Notes Download this
This happens in order for us to be able to identify the problem and fix it, and keep the data consistent with the mater that has http://www.ducea.com/2008/02/13/mysql-skip-duplicate-replication-errors/ sent the query. You can skip such errors, even if this is not recommended, as long as you know really well what are those queries and why they are failing, etc. https://www.drupal.org/node/998474 For example you can skip just one query that is hanging the slave using: 1 mysql>SET GLOBAL SQL_SLAVE_SKIP_COUNTER=1; START SLAVE; There might be cases where you will want to skip more duplicate entry queries. For example you might want to skip all duplicate errors you might be getting (output from show slave status;): 1 "1062 | Error 'Duplicate entry 'xyz' for key 1' on query. Default database: 'db'. Query: 'INSERT INTO ..." If you are sure that skipping those errors will not bring your slave inconsistent and you want to skip them ALL, you would duplicate entry for add to your my.cnf: 1 slave-skip-errors = 1062 As shown above in my example 1062 is the error you would want to skip, and from here we have: _ Error: 1062 SQLSTATE: 23000 (ER_DUP_ENTRY) Message: Duplicate entry ‘%s’ for key %d_ You can skip also other type of errors, but again don’t do this unless you understand very well what those queries are and what impact they have on your data: 1 slave-skip-errors=[err_code1,err_code2,...|all] and for the error codes you can see them all here … (you will see them in your _show slave statu_s; also). Posted by Marius Ducea Feb 13th, 2008 posted in: tips & tricks tagged with: mysql, tips Tweet « Linux Tips: find all files of a particular size Increase PHP memory limit » Comments Please enable JavaScript to view the comments powered by Disqus. About Me My name is Marius Ducea. I am an experienced Infrastructure Developer based in the SF Bay Area. You can find out more about me here. I blog here mostly about things I don't want to forget ;) Do you have an interesting project idea? Or
that make connections all over the world. Join today Download & Extend Drupal Core Distributions Modules Themes Issues Duplicate entries in search_index (MySQL error ERROR 1062) Closed (won't fix)Project:Drupal coreVersion:6.22Component:search.modulePriority:MajorCategory:Bug reportAssigned:UnassignedIssue tags:utf8_unicode_ciMySQL 5.1utf8_general_ciReporter:asbCreated:December 14, 2010 - 23:33Updated:January 7, 2014 - 18:58 Log in or register to update this issue Jump to:Most recent comment I'm trying to restore a MySQL database dump from a Drupal 6.19 installation; restoring the dump fails because of "duplicate entry" errors: mysql -u root -p database_drupal < dbcontent.sql Enter password: [...] ERROR 1062 (23000) at line 5751: Duplicate entry 'das-415-node' for key 'word_sid_type' Lines 5750 and 5751 read: $ sed -n '5750p' dbcontent.sql | more /*!40000 ALTER TABLE `search_index` DISABLE KEYS */; ... $ sed -n '5751p' dbcontent.sql | more INSERT INTO `search_index` VALUES ('situation',1,'node',0.816452), [...] (a very long line follows). As it seems, line 5751 trieds to insert duplicates into the search index. Simply deleting the "damaged" line does not help: $ sed '5752d' dbcontent.sql > dbcontent_fixed.sql ... $ mysql -u root -p database_drupal < dbcontent_fixed.sql Enter password: [...] ERROR 1062 (23000) at line 5751: Duplicate entry 'das-415-node' for key 'word_sid_type' ... $ mysql -u root -p cinedat_drupal < dbcontent_fixed_3.sql Enter password: [...] ERROR 1062 (23000) at line 5752: Duplicate entry 'daß-3230-node' for key 'word_sid_type' I tried to delete line after line a couple of times, but there are always duplicates left. As it seems there is no easy way to restore this database dump. That's quite bad as it renders database backups quite useless for normal users. Table structure of 'search_index' from the live database (not the dump): mysql> describe search_index -> ; +-------+------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+------------------+------+-----+---------+-------+ | word | varchar(50) | NO | MUL | | | | sid | int(10) unsigned | NO | MUL | 0 | | | type | varchar(16) | YES | | | | | score | float | YES | | NULL | | +-------+------------------+------+-----+---------+-------+ 4 rows in set (0.00 sec) Strangely I seem to be unable to locate the duplicates in the live database: mysql> ALTER IGNORE TABLE search