Get last inserted auto increment using php?
Moderator: General Moderators
Get last inserted auto increment using php?
I've heard of using "mysql_insert_id", but is that the best way of getting the id of the last inserted row?
The easiest way for an auto-increment is using max():
That's assuming you're using an id field that auto increments... which makes the last inserted the largest number.
Code: Select all
SELECT max( id ) FROM `table`That's assuming you're using an id field that auto increments... which makes the last inserted the largest number.
Re: Get last inserted auto increment using php?
YesCitizen wrote:I've heard of using "mysql_insert_id", but is that the best way of getting the id of the last inserted row?
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
That's a waste of a query.Chalks wrote:The easiest way for an auto-increment is using max():Code: Select all
SELECT max( id ) FROM `table`
That's assuming you're using an id field that auto increments... which makes the last inserted the largest number.
-
chadhaajay
- Forum Newbie
- Posts: 2
- Joined: Sat Jul 28, 2007 4:45 am
I agree with both appraoches. sometimes using a query can be of best use.
Sincerely,
Ajay Chadha (Director)
Chadha Software Technologies
Knowledge Base Software - PHPKB
Search Scripts - Scripts Resource Directory
Sincerely,
Ajay Chadha (Director)
Chadha Software Technologies
Knowledge Base Software - PHPKB
Search Scripts - Scripts Resource Directory
Re: Get last inserted auto increment using php?
That will get the id of the auto_increment column value in the left insert made using your current connection. If you want to get the id of the last inserted row regardless of connection then you should use a query to fetch MAX(`id`).Citizen wrote:I've heard of using "mysql_insert_id", but is that the best way of getting the id of the last inserted row?
Which might also not be the value you expect if the record with the highest id is deleted. (possible even if unlikely).
e.g. the result of includes the auto_increment counter value.
e.g. the result of
Code: Select all
SHOW TABLE STATUS LIKE tablename- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Actually the surest way is to a database engine that supports transactions like InnoDB, and to use them.superdezign wrote:The surest way is to get the last entry that matches the data that you inserted into it (assuming all of them are generally unique). That's why I always use more information than I'll need, such as saving dates for almost all actions.
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Or if we could run a SELECT and an INSERT / UPDATE at the same time.onion2k wrote:Actually the surest way is to a database engine that supports transactions like InnoDB, and to use them.superdezign wrote:The surest way is to get the last entry that matches the data that you inserted into it (assuming all of them are generally unique). That's why I always use more information than I'll need, such as saving dates for almost all actions.
Code: Select all
INSERT INTO `table` SET `foo` = 1 ALSO SELECT LAST_INSERT_ID() FROM `table`- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm