i have tables forward and mail with the auto_increment field - mid i have the following sql queryes: insert into mail(mid,gdate,folder,txt,html,email,subj,date,uid,size,attach, attachbefore, attachafter,message,flag)values(NULL,now(),1,'"+g_text+"','"+g_html+"','"+g_from+"','"+g_subject+"','"+szDate+"',"+buf+","+buf1+","+buf2+","+szfirstattach+","+szlastattach+",'"+g_header.c_str()+"',"+flag[0]+")"; insert into forward(fwuid,message,email,user,subj,date,uid,mid,size,attach)values(NULL,'"+message+"','"+g_from+"','','"+g_subject+"','"+szDate+"',"+buf+",last_insert_id(),"+szmessagesize+","+buf2+")"; But some times last_insert_id() returns 0; Any suggestions??