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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:59:48+00:00 2026-06-18T14:59:48+00:00

I can’t figure out why the id returned with mysqli_insert_id() is different than my

  • 0

I can’t figure out why the id returned with mysqli_insert_id() is different than my expected id. Here is my code:

$query = "INSERT INTO payments_table (payment_type, payment_number, payment_cost, insertion_date) VALUES ( '$paymentType', '$paymentNumber', '$totalCost', NOW())";
$data = mysqli_query($dbc, $query);
$currentPayerID = mysqli_insert_id($dbc);

//DEBUG
echo 'current payerID is: ' . $currentPayerID;

Since there are currently 2 rows, I expect it to echo “current payerID is 3” after the next execution. However, after executing this code, it echoes the following:
“current payerID is 21”

A couple notes:

  1. This ‘payments_table’ table has a column named “payer_id” that is a
    primary key and has the AUTO_INCREMENT attribute.
  2. I’ve “truncated” this table(I believe this is the same as emptying it out).

I searched throughout stackoverflow, but some of the answer don’t believe that what I’m finding is possible. I appreciate any generous help!

  • 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-06-18T14:59:50+00:00Added an answer on June 18, 2026 at 2:59 pm

    mysqli_insert_id returns the last AUTO_INCREMENT value – if you’re inserting to another table after payments_table but before calling mysqli_insert_id you may be getting an unexpected value.

    Also an AUTO_INCREMENT column does not correspond with the number of rows in the database table, but the total number of rows over the lifetime of the table. More precisely, MySQL maintains a counter variable that is incremented each time a new row is created.

    The following query will show the next expected AUTO_INCREMENT value for a table:

    SELECT AUTO_INCREMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'my_database_name' AND TABLE_NAME = 'my_table_name';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone tell me where I'm going wrong here? basically I've written some code
Can someone help me figure out why my logo image isn't resizing according the
Can any one tell, how to get the result of LINQ query contains group
Can we change the default action of the edit selected row button? Here is
Can someone thoroughly explain the last line of the following code: def myMethod(self): #
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Can someone please help me find the memory leak which is occurring here? I
Can we use javap in our own java code in a programmable way? for
Can someone please explain how this code is working for me? I don't understand
Can I write this with less code, also is .click(function preferred for mobile or

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.