Reputation: 1080
I have a table which has many columns and all can have duplicate values except one column 'id'. This 'id' is primary key in my table. This 'id' is automatically incremented for every new record.Now what I want is as soon as I enter a new record I want to add some details regarding this record in another table and for that I need this primary key. So how do I find the value of'id' for last record entered?
I am using php
Upvotes: 0
Views: 52
Reputation: 2920
Infact, to make an answer out of my comments above:
If you check this page : https://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html the important part is abstracted below.
Use LAST_INSERT_ID()
"For LAST_INSERT_ID(), the most recently generated ID is maintained in the server on a per-connection basis. It is not changed by another client. It is not even changed if you update another AUTO_INCREMENT column with a nonmagic value (that is, a value that is not NULL and not 0). Using LAST_INSERT_ID() and AUTO_INCREMENT columns simultaneously from multiple clients is perfectly valid. Each client will receive the last inserted ID for the last statement that client executed."
Upvotes: 2
Reputation: 20071
You can get the last id entered in a database using mysqli_insert_id
eg:
$id = mysqli_insert_id($connection);
http://php.net/manual/en/mysqli.insert-id.php
Upvotes: 1