{"id":26687,"date":"2016-09-14T09:14:14","date_gmt":"2016-09-14T07:14:14","guid":{"rendered":"https:\/\/mamchenkov.net\/wordpress\/?p=26687"},"modified":"2016-09-14T09:14:14","modified_gmt":"2016-09-14T07:14:14","slug":"mysql-php-and-integrity-constraint-violation-1062-duplicate-entry","status":"publish","type":"post","link":"https:\/\/mamchenkov.net\/wordpress\/2016\/09\/14\/mysql-php-and-integrity-constraint-violation-1062-duplicate-entry\/","title":{"rendered":"MySQL, PHP and &#8220;Integrity constraint violation: 1062 Duplicate entry&#8221;"},"content":{"rendered":"<!-- google_ad_section_start -->\n<p><a href=\"http:\/\/afilina.com\/\">Anna Filina<\/a> blogs about <a href=\"http:\/\/afilina.com\/mysql-duplicate-not-really\/\">an interesting problem<\/a> she encountered with when working on a PHP and MySQL project:<\/p>\n<blockquote><p>MySQL was complaining about \u201cIntegrity constraint violation: 1062 Duplicate entry\u201d. I had all the necessary safeguards in my code to prevent duplicates in tha column.<\/p>\n<p>I gave up on logic and simply dumped the contents of the problematic column for every record. I found that there was a record with and without an accent on one of the characters. PHP saw each as a unique value, but MySQL did not make a distinction, which is why it complained about a duplicate value. It\u2019s a good thing too, because based on my goal, these should have been treated as duplicates.<\/p><\/blockquote>\n<p>She also mentions two possible solutions to the problem:<\/p>\n<blockquote><p>My solution was to substitute accented characters before filtering duplicates in the code. This way, similar records were rejected before they were sent to the database.<\/p><\/blockquote>\n<p>and<\/p>\n<blockquote><p>As pointed out in the comments, a more robust and versatile solution would be to check the collation on the column.<\/p><\/blockquote>\n<p>I&#8217;m sure this will come in handy one day.<\/p>\n<!-- google_ad_section_end -->\n","protected":false},"excerpt":{"rendered":"<!-- google_ad_section_start -->\n<p>Anna Filina blogs about an interesting problem she encountered with when working on a PHP and MySQL project: MySQL was complaining about \u201cIntegrity constraint violation: 1062 Duplicate entry\u201d. I had all the necessary safeguards in my code to prevent duplicates in tha column. I gave up on logic and simply dumped the contents of the &hellip; <a href=\"https:\/\/mamchenkov.net\/wordpress\/2016\/09\/14\/mysql-php-and-integrity-constraint-violation-1062-duplicate-entry\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">MySQL, PHP and &#8220;Integrity constraint violation: 1062 Duplicate entry&#8221;<\/span><\/a><\/p>\n<!-- google_ad_section_end -->\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"MySQL, PHP and \"Integrity constraint violation: 1062 Duplicate entry\" #PHP #MySQL #unicode #databases #WebDev","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_links_to":"","_links_to_target":""},"categories":[1,18,62,1334],"tags":[1559,3066,38,23,1330],"keyring_services":[],"class_list":["post-26687","post","type-post","status-publish","format-standard","hentry","category-general","category-programming","category-technology","category-web-work","tag-databases","tag-mysql","tag-php","tag-unicode","tag-web-development"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[{"id":12480,"url":"https:\/\/mamchenkov.net\/wordpress\/2010\/04\/21\/unit-tests-with-cakephp\/","url_meta":{"origin":26687,"position":0},"title":"Unit tests with CakePHP","author":"Leonid Mamchenkov","date":"April 21, 2010","format":false,"excerpt":"I've spent a large part of yesterday setting up the testing environment for a CakePHP project.\u00a0 As always, every time I do something that I have done before, I wanted to do it better, using all the experienced that was acquired previously.\u00a0 And this often leads to the discovery of\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26688,"url":"https:\/\/mamchenkov.net\/wordpress\/2016\/09\/16\/bitbucket-pipelines-and-docker-for-php-developers\/","url_meta":{"origin":26687,"position":1},"title":"BitBucket Pipelines and Docker for PHP Developers","author":"Leonid Mamchenkov","date":"September 16, 2016","format":false,"excerpt":"I've been meaning to look into Docker for a long while now. \u00a0But, as always, time is the issue. \u00a0In the last couple of days though I've been integrating BitBucket Pipelines into our workflow. \u00a0BitBucket Pipelines is a continuous integration solution, which runs your project tests in a Docker container.\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":27341,"url":"https:\/\/mamchenkov.net\/wordpress\/2017\/02\/12\/github-to-mysql\/","url_meta":{"origin":26687,"position":2},"title":"GitHub to MySQL","author":"Leonid Mamchenkov","date":"February 12, 2017","format":false,"excerpt":"GitHub to MySQL is a handy little app in PHP that pulls labels, milestones and issues from GitHub into your local MySQL database. \u00a0This is useful for analysis and backup purposes. There are a few example queries provided that show issues vs. pull requests,\u00a0average number of days to merge a\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2017\/02\/issues-vs-pull-requests-500x229.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":16547,"url":"https:\/\/mamchenkov.net\/wordpress\/2012\/07\/23\/economic-impact-of-open-source-on-small-business\/","url_meta":{"origin":26687,"position":3},"title":"Economic impact of open source on small business","author":"Leonid Mamchenkov","date":"July 23, 2012","format":"link","excerpt":"Economic impact of open source on small business Here are a few of the findings we derived from\u00a0Bluehost\u00a0data (an EIG company) and follow-on research: 60% of web hosting usage is by SMBs, 71% if you include non-profits. Only 22% of hosted sites are for personal use. WordPress is a far\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10151,"url":"https:\/\/mamchenkov.net\/wordpress\/2006\/05\/31\/daily-del-icio-us-bookmarks\/","url_meta":{"origin":26687,"position":4},"title":"Daily del.icio.us bookmarks","author":"Leonid Mamchenkov","date":"May 31, 2006","format":false,"excerpt":"Shared bookmarks for del.icio.us user tvset on 2006-05-30 MySQL Performance Blog \u00c2\u00bb INSERT ON DUPLICATE KEY UPDATE and summary counters. Tagged as: code databases mysql optimization performance programming sql tips tricks Read\/WriteWeb: Worldwide Internet Penetration is just 15% Tagged as: statistics stats web world","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":9256,"url":"https:\/\/mamchenkov.net\/wordpress\/2005\/07\/21\/daily-del-icio-us-bookmarks\/","url_meta":{"origin":26687,"position":5},"title":"Daily del.icio.us bookmarks","author":"Leonid Mamchenkov","date":"July 21, 2005","format":false,"excerpt":"Shared bookmarks for del.icio.us user tvset on 2005-07-21 NewsForge | Hooking OOo to MySQL Tagged as: computers databases desktop mysql openoffice technology ALLCY.COM :: WELCOME :: -- All Cyprus: people and business finder and government pages Tagged as: cyprus government references search Writing Secure PHP - PHP - ILoveJackDaniels.com Tagged\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/26687","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/comments?post=26687"}],"version-history":[{"count":0,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/26687\/revisions"}],"wp:attachment":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/media?parent=26687"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/categories?post=26687"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/tags?post=26687"},{"taxonomy":"keyring_services","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/keyring_services?post=26687"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}