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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:03:35+00:00 2026-05-26T20:03:35+00:00

If I begin a transaction, and make multiple INSERT s in it (pre- COMMIT

  • 0

If I begin a transaction, and make multiple INSERTs in it (pre-COMMIT), mysql_insert_id() doesn’t seem to update following each one.

Is this the expected behavior? If so, what can I do to get the ID’s for the things I just inserted?

  • 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-26T20:03:36+00:00Added an answer on May 26, 2026 at 8:03 pm

    Your assumption is wrong. Here is code that proves it:

    mysql_query('CREATE TABLE `test` (
                   `ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
                   `Name` varchar(255) NOT NULL,
                    PRIMARY KEY (`ID`)
                 ) ENGINE=InnoDB') or die(mysql_error());
    mysql_query('SET AUTOCOMMIT=0') or die(mysql_error());
    mysql_query('START TRANSACTION') or die(mysql_error());
    mysql_query("INSERT INTO test VALUES (NULL, 'Martin')") or die(mysql_error());
    echo mysql_insert_id().'<br />';
    mysql_query("INSERT INTO test VALUES (NULL, 'Dani')") or die(mysql_error());
    echo mysql_insert_id().'<br />';
    mysql_query("INSERT INTO test VALUES (NULL, 'Pesho')") or die(mysql_error());
    echo mysql_insert_id().'<br />';
    mysql_query('COMMIT') or die(mysql_error());
    mysql_query('SET AUTOCOMMIT=1') or die(mysql_error());
    

    The output of this simple code is:

    1
    2
    3
    

    The interesting thing is that even if you rollback the transaction (instead of committing it), the result will be the same and the values for these ids will never be used again. So the next insert will start with 4.

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

Sidebar

Related Questions

I have two PL/SQL Stored procedure each handling its own Transaction (Begin/Commit and Rollback
Consider the following SQL: BEGIN TRAN SET TRANSACTION ISOLATION LEVEL READ COMMITTED INSERT Bands
Is it possible to use the SQL BEGIN TRANSACTION, COMMIT TRANSACTION, ROLLBACK TRANSACTION when
Say a stored procedure on MSSQL Server uses an SQL transaction using BEGIN TRANSACTION/COMMIT
What is the equivalent to the following SQL Server statements in DB2? Begin Transaction
I want to implement an atomic transaction like the following: BEGIN TRAN A SELECT
I need to do transactions (begin, commit or rollback), locks (select for update). How
Lets say I open a transaction and run update queries. BEGIN TRANSACTION UPDATE x
Consider that I have a transaction: BEGIN TRANSACTION DECLARE MONEY @amount SELECT Amount AS
When I call the method session.begin transaction as follows: //session factory is instantiated via

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.