I need to know if theres a function that is simmilar to mysql_insert_id function

where it gets the last inserted id

but the thing is my id is not auto increment

i tried doing the select statement but not working

my scenario is:

i am updating a record from one table after inserting a new record in another table

so insert then update in one script.

thanks in advance!

5 Years
Discussion Span
Last Post by pritaeas

i guess that your scenario is that you are updating 2nd table after inserting in 1st table???


it doesn't matter if your incrementing your id or not, mysql_insert_id() always return the last inserted id

no it returns 0 if column is not auto incremented


no it returns 0 if column is not auto incremented

it's because it is not incremented! did you get my point? because he did not increment his primary id, all his id including his last id is zero


From the manual:
"Retrieves the ID generated for an AUTO_INCREMENT column by the previous query (usually INSERT)."

I've highlighted the important parts:
1. If he is not using an auto increment ID column, then this function returns zero.
2. You have to call this function directly after the INSERT query, otherwise it returns zero.

If the OP is not using an auto incremented ID, he has to query the table with a SELECT to get the highest (most likely last) ID, or any other logic that matches his inserting logic.

Edited by pritaeas: n/a


Thats the reason why I indicated that my id is not auto increment

cause I know it will return 0 or false

Ive also tried doing select statement after insert then update but not getting any luck

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.