dbTalk Databases Forums  

about the last_insert_id() error

comp.databases.mysql comp.databases.mysql


Discuss about the last_insert_id() error in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
white_ideal
 
Posts: n/a

Default about the last_insert_id() error - 06-19-2011 , 08:03 PM






hi, everyone. everytime it would return 0 after I insert a row and use
the get_last_id() to get the inserted auto_increment id. I don't know
what's the reasion and it's so confusion. Can you give some helps?

and the following the process to reproduce.
1. open the "mysql query browser";
2. CREATE TABLE test_tab (`f1` INTEGER NOT NULL AUTO_INCREMENT,`f2`
VARCHAR(30),PRIMARY KEY (`f1`));
3. insert into test_tab(f2) values("a");
4. select * from test_tab;
f1 f2
1 a
5. select last_insert_id();
LAST_INSERT_ID()
0
6. insert into test_tab(f2) values("b");
7. select * from test_tab;
f1 f2
1 a
2 b
8. select last_insert_id();
LAST_INSERT_ID()
0

The results are error of course, the first "select last_insert_id()"
should return 1 and the second should be 2. But the results is correct
in "mysql command line client".

Maybe some of you guys can help me reslove this problem and any helps
for it would be appreciated.

Thanks.

Reply With Quote
  #2  
Old   
Lennart Jonsson
 
Posts: n/a

Default Re: about the last_insert_id() error - 06-20-2011 , 01:46 AM






On 2011-06-20 03:03, white_ideal wrote:
Quote:
hi, everyone. everytime it would return 0 after I insert a row and use
the get_last_id() to get the inserted auto_increment id. I don't know
what's the reasion and it's so confusion. Can you give some helps?
[...]

Quote:
6. insert into test_tab(f2) values("b");
7. select * from test_tab;
f1 f2
1 a
2 b
8. select last_insert_id();
LAST_INSERT_ID()
0

You must invoke last_insert_id() immediate after the insert, see:

http://dev.mysql.com/doc/refman/5.5/...insert-id.html


/Lennart

Reply With Quote
  #3  
Old   
Axel Schwenke
 
Posts: n/a

Default Re: about the last_insert_id() error - 06-20-2011 , 02:20 AM



white_ideal <whiteideal (AT) gmail (DOT) com> wrote:

Quote:
hi, everyone. everytime it would return 0 after I insert a row and use
the get_last_id() to get the inserted auto_increment id. I don't know
what's the reasion and it's so confusion. Can you give some helps?

and the following the process to reproduce.
1. open the "mysql query browser";
There is the reason.

LAST_INSERT_ID() and friends rely on information in the current
connection ("session context" or "connection scoped state").
The Query Browser however opens a new connection for each operation
and closes it immediately after.

Better use a real SQL client. Like mysql(.exe)

More details:

http://mysqldump.azundris.com/archiv...-in-MySQL.html


XL

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.