INSERT .. ON DUPLICATE query issue

Discussion in 'PHP' started by Shoe ButtBack, Mar 23, 2010.

  1. #1
    I basically want to insert a record and return the id. I'm using mysql_insert_id().

    However, I'm stuck at one part. If the record is a duplicate, it returns a THE NEXT autoincrement id.

    I want it to return the id of the record thats a duplicate.

    So if record exists and its id is 57, I want it to return 57, OTHERWISE return the new id of the inserted element.

    Any help?

    Armin
     
    Shoe ButtBack, Mar 23, 2010 IP
  2. Xuhybrid

    Xuhybrid Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You could run a query to search for a duplicate before you insert.
    Then you can easily retrieve that variable, and still continue to insert and grab mysql_insert_id();
     
    Xuhybrid, Mar 23, 2010 IP
  3. Alex Roxon

    Alex Roxon Active Member

    Messages:
    424
    Likes Received:
    11
    Best Answers:
    7
    Trophy Points:
    80
    #3
    Or instead of mysql_insert_id, afterwards just run a query, order by ID in ascending order and limit by 1 result. Would return the ID you're after.
     
    Alex Roxon, Mar 23, 2010 IP
  4. Shoe ButtBack

    Shoe ButtBack Active Member

    Messages:
    179
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    68
    #4
    Hi guys, thanks for the help, but the whole point of this is to AVOID extra queries. Its easy to query for the ID, but my question is WHY does it not return the existing id when a duplicate record exists, instead it returns a NEW auto_increment id, even though the record already exists.
     
    Shoe ButtBack, Mar 23, 2010 IP
  5. Alex Roxon

    Alex Roxon Active Member

    Messages:
    424
    Likes Received:
    11
    Best Answers:
    7
    Trophy Points:
    80
    #5
    Meaning, it grabs the id from your last query. Does that help explain why? :)
     
    Alex Roxon, Mar 23, 2010 IP
  6. Shoe ButtBack

    Shoe ButtBack Active Member

    Messages:
    179
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    68
    #6
    Yes, I know that. I wish this was the case. mysql_insert_id in my case grabs the id of the NEW RECORD that is NOT BEING INSERTED.

    If there are records 1,2,3 and I'm trying to insert record 2, it would return 4!!! NOT 2!!
     
    Shoe ButtBack, Mar 23, 2010 IP
  7. Xuhybrid

    Xuhybrid Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Am i missing a point to this? php built in a practical, useful function. If you want another php function to do the leg work, then make one.

    Surely mysql returns an error when you try to insert over an existing id. You might then be able to parse that error to get the id. Also, if you are trying to insert over a duplicate id, then you must already know it. Your method is not logical, perhaps i have misunderstood.
     
    Xuhybrid, Mar 23, 2010 IP