List:General Discussion« Previous MessageNext Message »
From:Michael Dykman Date:December 27 2009 5:00pm
Subject:Re: last_insert_id
View as plain text  
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
<victorsubervi@stripped> wrote:
> On Sun, Dec 27, 2009 at 11:27 AM, Mattia Merzi <mattia.merzi@stripped>wrote:
>
>> 2009/12/27 Victor Subervi <victorsubervi@stripped>:
>> > 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
 - mdykman@stripped

"May you live every day of your life."
    Jonathan Swift

Larry's First Law of Language Redesign: Everyone wants the colon.
Thread
last_insert_idVictor Subervi27 Dec
  • Re: last_insert_idMattia Merzi27 Dec
    • Re: last_insert_idVictor Subervi27 Dec
      • Re: last_insert_idMichael Dykman27 Dec
        • Re: last_insert_idVictor Subervi27 Dec
          • Re: last_insert_idGary Smith27 Dec
            • Re: last_insert_idVictor Subervi27 Dec
            • Re: last_insert_idCarsten Pedersen27 Dec
  • Re: last_insert_idSteve Edberg27 Dec
    • Re: last_insert_idGary Smith27 Dec
      • Re: last_insert_idMark Goodge27 Dec