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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:08:46+00:00 2026-05-26T08:08:46+00:00

Hi I am implementing transaction using php and mysql. Below is my code where

  • 0

Hi I am implementing transaction using php and mysql. Below is my code where i am inserting into test in which id is an autoincrement value.

try{
  $dbh->beginTransaction();

  $stmt = $dbh->prepare("INSERT INTO test(id,name) VALUES (?,?)")          
  $stmt->execute(array(null ,$Name));

   -----
   ----- and some more queries

  $dbh-> commit();
  $dbh=null;
}

catch(Exception $e)
{
  $dbh->rollBack();
  $dbh=null;
 }

The problem i am facing is each time when transaction is rolledBack due to some exception the next time when i run the query insert is working based on the autoincrement value assigned when it was last run

for eg:

Run 1:
insert query — id value is 1 and the transaction is rolledBack

Run2:
insert query — Now it is taking id value as 2 although my previous transaction was rolledBack

Could anyone help me with this?

  • 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-26T08:08:47+00:00Added an answer on May 26, 2026 at 8:08 am

    That happened because autoincrement value is shared among all transactins and by its nature it is DDL, not DML. Thus it cannot be rolled back (as well as any other DDL, like TRUNCATE or ALTER cannot be rolled back).

    PS: autoincrement PK is a surrogate key, which means that you shouldn’t care of what value it keeps. You should be satisfied with just a fact that it is unique. That’s all. If you need to maintain some “order” – create another column and do that manually.

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

Sidebar

Related Questions

implementing publishActivity in PHP using the REST API using this code: $activity = array(
I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I
I'm importing some content into a Nested Tree model, and I'm implementing a Transaction
I am implementing language files for my PHP application. I am using constants to
I have a j2ee application running on spring framework. I am implementing a transaction
Which is the best way of implementing a language translation for mutli -lang site.
I am implementing a WCF service that uses transaction propagation. The ASP .nET Security
I'm implementing a Java-based system which needs accessing both the native and networked filesystems.
I'm currently implementing Interceptors using Castle DynamicProxy. I require the interceptor to pick up
I'm using django-transmeta (couldn't get anything else working better with django 1.2.5) which creates

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.