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

  • Home
  • SEARCH
  • 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 7665101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:28:31+00:00 2026-05-31T14:28:31+00:00

I have a table in MySQL that for compatibility issues we assigned specific Primary

  • 0

I have a table in MySQL that for compatibility issues we assigned specific Primary Keys, therefore they can not be auto incremented.

Every time we insert a new topple (in PHP) we need to get back the latest ID inserted. It might sound stupid. but we do not generate the ID from PHP, it comes from an generic AJAX function, therefore we don’t know the name of the variable containing the Primary Key, it always changes.

Is there a way, using PHP or MYSQL, to obtain the latest ID inserted for a primary key that does not auto-increment?

Unfortunately mysql_insert_id() and last_insert_id() do not work without auto_increment.

Thanks!

  • 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-31T14:28:33+00:00Added an answer on May 31, 2026 at 2:28 pm

    Head down to the comments section on this page.
    http://php.net/manual/en/function.mysql-insert-id.php
    And you should see some methods that might work for you.

    I would try inserting the specific time_stamp and then calling that time_stamp insert again.

    Dave does a good job of explaining it in this comment. Quoting him..


    There is a lot of incorrect info here on “don’t use AI” and “using max
    is equivalent”. Then you have people improperly advising you to use
    psuedo-random numbers

    If you’re really worried about the AI field not returning because of
    the inherent race conditions, just do a select statement based on the
    vars you just input. If your vars are not unique, DON’T use these
    psuedo-random numbers. When you have enough iterations, the
    probability that the one of these randoms becomes a duplicate gets
    pretty high.

    Instead, just use the unix timestamp. However, don’t use
    UNIX_TIMESTAMP() in your query, because if you do, when you run your
    select statement right after, there’s a possibility that you’ll end up
    with a different timestamp.

    Since date() continues to count up during the execution of the script,
    simply store date(U) into a variable or definition. Then insert and
    select based on that. Assuming you’re not using mysql procedures:

    <?php
    
    define(UNIX_TIMESTAMP, date('U'));
    
    $db->query("insert into table values('', 'a', 'b', 'c', 'd', '".UNIX_TIMESTAMP."'");
    $res = $db->query("select id from table where a_col = 'a' and b_col = 'b' and c_col = 'c' and d_col = 'd' and temp_id = 'UNIX_TIMESTAMP'");
    
    //...
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL table that defines values for a specific customer type. |
I have a table in MySQL that has a primary key: mysql> desc gifts;
I have a mysql table that holds comments. The comments can have sub-comments, but
I have a MySQL table of users whose primary key is an auto-incrementing integer
I have a MySQL table that looks like this: id (int primary) name (text)
I have a MySQL table with a primary key field that has AUTO_INCREMENT on.
I have a table in MySQL that has 3 fields and I want to
I have a table in MySQL That looks like the following: date |storenum |views
I have mysql table that has a column that stores xml as a string.
I have an ISAm table in mySql that was created similar to this: create

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.