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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:26:32+00:00 2026-05-25T01:26:32+00:00

I need to get the last inserted id of table that have multi-column primary

  • 0

I need to get the last inserted id of table that have multi-column primary keys.

  • Those tables does not have AUTOCOUNT column.
  • I’m using parametrized queries (arbitrary order)
  • Using PHP (5.3) and MySQLi module
  • Arbitrary INSERT SQL Query. (In any format)

For example:

Table: foo
Primary key: [ group_id , user_id ]
Query: INSERT INTO foo (group_id, user_id, name, email) VALUES (?, ?, ?, ?);
Parameters: array(34,15,"John","john@example.com")

Result: $last_id = $mysqli->insert_id ?: getInsertedId();

34,15

At this moment I have a function named getPK(), which returns me:

array("group_id","user_id");

What I need now is to implement getInsertedId(). Which could be the easy way to do it without using an SQL parser?

I’m pretty sure there is already an answer for this question but I couldn’t find anything….

UPDATE

The reason of why I’m asking this question is because I have a class which control everything related with the MySQL database (part of a personal framework). I have one method that is called set() in which queries (like UPDATE, INSERT, DELETE, etc.) are passed. I have other specific methods like insert() in which arrays are passed.

I have a variable in which I store the last_inserted_id. That variable can be called anytime later. I have many tables in different systems that have multiple-primary-keys.
When using the insert() method, I have no problem to set the last_inserted_id value, but when some systems use the set() method, I can not retrieve that value and I have to return 0. I would like to change that behavior.

I wanted to simplify my explanation with the above example.

UPDATE 2

Not all systems are controlled by myself. For example, one of the systems call a soap method in which a query is sent to be executed (any kind of query). Those are handled by set() method. Then there is other soap method in which the last id is retrieved. For consistency I would like to return that value.

  • 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-25T01:26:33+00:00Added an answer on May 25, 2026 at 1:26 am

    As Phil pointed out in a comment, MySQL LAST_INSERT_ID() can only return auto-generated values (AUTO_INCREMENT).

    Furthermore, you can’t have an auto_increment on multiple columns: if you have a multiple primary key, then the auto_increment is possible on only 1 column.

    In conclusion, you can’t get the key inserted using LAST_INSERT_ID() in your case.

    A solution would be to handle that case in PHP, so that you getInsertedId() method returns the values of primary keys that was given for the insert.

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

Sidebar

Related Questions

I need to get the td element of a table. I do not have
In a mysql table for a date column(login_time), i need to get the logins
I have a .txt file where I need to get rid of the last
I need to get the first and last day of a month in the
I need to get some information that is contained in the MFT on a
I have a simple table called that contains share prices in MySQL: Table `share_prices`
I have a MySQL question that I think must be quite easy. I need
I need to get the number of lines in a UILabel, so that I
Hi i have a form (X) that inserts data into a mysql table (A)
I have spent the last several hours trying to get this to work and

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.