有二个表格分别为thead和posts,一个类似留言板的设计,(discuz,phpwind里边也是相同名字的表)thead
tid auto_increment primary key,
time int,
..........posts
pid auto_increment primary key,
tid int, 
time int,
.....现在就是,在thead表里边插入一行数据后,要得到自动增长的thead.tid的值,然后把thead.tid的值插入到posts表格里的posts.tid里边,有没有好的方法实现这个??目前能想到的只有,,:注$phpvar为php端生成的时间戳
lock table thead;
insert into thead(tid,time,...) value(null,$phpvar,)
set @AA=last_insert_id(); ///这里或者select @AA:=tid from thead where time=$phpvar; 
insert into post(pid,tid,...) value(null,@AA,...);
unlock tables

解决方案 »

  1.   

    可以直接:
    insert into post(pid,tid,...) value(null,last_insert_id(),...);
      

  2.   

    实际上我有些不想锁定表,有没有更好的方式实现,也不知道discuz是怎么弄的
      

  3.   

    在同一个connectionk中你可以直接用 last_insert_id(),不需要锁定表啊。discuz中的表设计好象不是这样,你可以直接下载一个看一下。
      

  4.   

    担心当有不同客户端同时往thead里插入值时,会出错,原来想太多了,,discuz里边的表我看过了,就是不知php文件里边怎么写的,
      

  5.   

    不会的,因为你的PHP每次都会是一个新的connection,而last_insert_id(),是connection级的。不会受另一个connection的影响。