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 4605934
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:29:52+00:00 2026-05-22T00:29:52+00:00

Okay first of all, I admit I took elements from this question here: How

  • 0

Okay first of all, I admit I took elements from this question here: How to get result of Insert

Anyways, I have two tables and in the second table there is a foreign key column for table A.

Table A:

id       Name
----------------
1        Name1
2        Name2

Table B:

id       Name      Parent
-------------------------
1        John      1

The problem is I need the ID of anything inserted into table A, to use for the foreign key in table B.

The accepted answer was the following bit of code in PHP:

$res = mysql_query('SELECT LAST_INSERT_ID()');
$row = mysql_fetch_array($res);
$lastInsertId = $row[0];

I’ve seen similar code elsewhere, but isn’t there a synchronization issue here? Couldn’t it be possible for a different SQL insert to occur right in between one SQL insert and getting the ID with SELECT LAST_INSERT_ID()? Therefore ending up with the wrong ID?

Thanks,

Sam

  • 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-22T00:29:53+00:00Added an answer on May 22, 2026 at 12:29 am

    Either using the function mysql_insert_id() or the method you described in your question are reliable and concurrency safe. Both methods use the active connection, the concurrency issue you’re pointing to is anticipated and dealt with internally.

    If you Google search for this question, you’ll see some people advocate using ‘SELECT MAX(id) FROM some_table`, which DOES have the concurrency problem you’re worried about and should not be used. Either of the aforementioned two methods don’t have this problem and are equally “best practice”.

    You can read some more discussion about the MySQL function here: http://dev.mysql.com/tech-resources/articles/storage-engine/part_3.html

    The relevant bit from that article:

    SELECT LAST_INSERT_ID();

    This is a special construct, and again
    it’s perfectly safe without manual
    locks, no matter how many concurrent
    connections the server has.

    In the PHP documents, you can see that mysql_insert_id() takes a connection as an argument, or it assumes the last opened one: http://php.net/manual/en/function.mysql-insert-id.php

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

Sidebar

Related Questions

Okay I will first admit I don't know the proper terminology for all this
Okay this is a fairly broad question. This is my first App and I'm
Okay first let me tell you the story behind the question. We have a
First of all, I am using PhpMyAdmin , is this okay or not? Because
First of all, i'm a Graphic designer so please ignore if this programming question
Okay so I have two import pieces of code involved in this. This first
Okay, I've looked all over the internet for a good solution to get PHP
Okay, here's the scenario. I have a utility that processes tons of records, and
First of all, im new here, and im (not) a pro ;) (but i
Okay, first, I'm a noob at android programming. I have taken some Java, but

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.