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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:57:37+00:00 2026-05-26T04:57:37+00:00

It seems there are two methods for this, the first one goes like this:

  • 0

It seems there are two methods for this, the first one goes like this:

$result = mysql_query("SHOW TABLE STATUS LIKE 'table_name'");
$row = mysql_fetch_array($result);
$nextId = $row['Auto_increment'];
mysql_free_result($result);

$sql = mysql_query("INSERT the information you want in the main table")

You can then insert $nextid as a foreign key in all your related tables.

The other method uses last insert id:

START TRANSACTION;
INSERT INTO files (file_id, url) VALUES (NULL, 'text.doc');
INSERT INTO grades (file_id, grade) VALUES (LAST_INSERT_ID(), 'some-grade');
COMMIT;

Are both of these thread safe? IE, if I have two php scripts running two mysql threads at the same time and they insert into the main table at the same time will the associated tables receive the correct primary id foreign key or could there be a mixup or collision?

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-26T04:57:37+00:00Added an answer on May 26, 2026 at 4:57 am

    Use the second (or mysql_insert_id()) as it is multi-user safe anyway. The transaction is only needed for consistency reasons but not for the LAST_INSERT_ID(). Furthermore, you should use mysqli_*() instead of mysql_*() as it is the improved database access library.

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

Sidebar

Related Questions

I have two tables kinda like this: // Person.Details Table: PersonID int [PK] |
There seems to be two major conventions for organizing project files and then many
It seems to me that there are two scenarios in which to use JOINs:
When invoking a WCF service asynchronous there seems to be two ways it can
It seems that these two operators are pretty much the same - is there
For iPhone game development, seems there are very like functions using CALayer and UIView,
I'm really pulling my hair out on this one, it seems that I'm having
LOTS of Default.png Q's, but I didn't read one like this: I see Default.png,
In my Asp.net MVC app, I have two methods on a controller, one for
I have an Android Honeycomb application with two activities; the first one has a

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.