Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6913403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:12:53+00:00 2026-05-27T09:12:53+00:00

This is kind of similar to this question: PHP MySQL INSERT fails due to

  • 0

This is kind of similar to this question:

PHP MySQL INSERT fails due to unique constraint

but I have a different twist. Let’s say I have a table with only one column. The column’s name is “title” and it has a unique constraint placed on it.

First I insert a row where title = “something”. The next time I try to insert “something” it will fail due to a unique key constraint (which is good). What I’d like to do is allow it to fail, and check the error code provided by mysql to ensure it failed due to a unique key constraint. (i.e. let the database handle the uniqueness, and I just handle the error code and tell the user that title already exists when the result comes back).

Is there a way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:12:54+00:00Added an answer on May 27, 2026 at 9:12 am

    http://dev.mysql.com/doc/refman/5.5/en/error-messages-server.html

    http://php.net/manual/en/function.mysql-errno.php

    I’ve had to do this in the past, and it’s not fun:

    if( mysql_errno() == 1062) {
        // Duplicate key
    } else {
        // ZOMGFAILURE
    }
    

    A note on programming style (Credits to jensgram from this answer)
    You should always seek to avoid the use of magic numbers. 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 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Kind of a basic question, but I have a page similar to this example:
The question I have is kind of simular to this one - MySQL select
Referring on this question , I have a similar -but not the same- problem..
This kind of question may be asked several times but my problem is different.
I've already asked a similar question but this is slightly different so i Thought
This is kind of similar to subversion merge - "has different repository root than"
This kind of code would normally work in PHP, but since the scope is
I have this kind of question. In my form, i got this as a
I know similar kind of question has been asked many times but seriously i
This Question is similar but the accepted answer solves it server side, I'm interested

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.