last_insert_id() returns the last id auto-incremented in *the current session*. If you disconnect and reconnect, it can not be retrieved.
- michael dykman On Sun, Dec 27, 2009 at 11:42 AM, Victor Subervi <victorsube...@gmail.com> wrote: > On Sun, Dec 27, 2009 at 11:27 AM, Mattia Merzi <mattia.me...@gmail.com>wrote: > >> 2009/12/27 Victor Subervi <victorsube...@gmail.com>: >> > mysql> select * from products; >> [...] >> > mysql> select last_insert_id() from products; >> [...] >> > Now, I was expecting 1, not 0! What up? >> >> [...] LAST_INSERT_ID() (no arguments) returns the first >> automatically generated value successfully inserted for >> an AUTO_INCREMENT column as a result of the most >> recently executed INSERT statement. [...] If no rows >> were (successfully) inserted, LAST_INSERT_ID() returns 0. >> > > But it *is* auto incremented! > > mysql> describe products; > +--------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------+------+-----+------------+----------------+ > | Field | > Type > | Null | Key | Default | Extra | > +--------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------+------+-----+------------+----------------+ > | ID | tinyint(5) > unsigned > | NO | PRI | NULL | auto_increment | > | SKU | > varchar(40) > | NO | UNI | NULL | | > | Category | > varchar(40) > | YES | | NULL | | > | Name | > varchar(50) > | NO | | NULL | | > | Title | > varchar(100) > | NO | | NULL | | > | Description | > mediumtext > | NO | | NULL | | > | Price | > float(8,2) > | YES | | NULL | | > | SortFactor | > int(4) > | YES | | 500 | | > | Availability | > tinyint(1) > | NO | | 1 | | > | OutOfStock | > tinyint(1) > | NO | | 0 | | > | ShipFlatFee | > float(5,2) > | NO | | 10.00 | | > | ShipPercentPrice | tinyint(2) > unsigned > | NO | | 5 | | > | ShipPercentWeight | tinyint(2) > unsigned > | NO | | 2 | | > | Associations | > varchar(40) > | NO | | NULL | | > | TempPrice | > tinyint(1) > | NO | | 1 | | > | LastDatePrice | > date > | NO | | 2000-01-01 | | > | Weight | > float(7,2) > | NO | | NULL | | > | Metal | enum('14k gold','18k gold','white > gold','silver','tungsten','titanium') > | NO | | NULL | | > | PercentMetal | tinyint(2) > unsigned > | NO | | NULL | | > | pic0 | > mediumblob > | YES | | NULL | | > | pic1 | > mediumblob > | YES | | NULL | | > | sizes | > set('Extra-small','Small','Medium','Large','XLarge','XXLarge','XXXLarge') > | YES | | NULL | | > | colorsShadesNumbersShort | > set('blue:333399','gray:465945','purple:50404D','navy-blue:CC7722','fuchsia:FF77FF','aqua:7FFFD4','maroon:B03060','black:0000FF','yellow:9ACD32') > | YES | | NULL | | > +--------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------+------+-----+------------+----------------+ > 23 rows in set (0.00 sec) > > mysql> > -- - michael dykman - mdyk...@gmail.com "May you live every day of your life." Jonathan Swift Larry's First Law of Language Redesign: Everyone wants the colon. -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe: http://lists.mysql.com/mysql?unsub=arch...@jab.org