其实不用查询阿,用完insert以后用mysql_insert_id()就可以得到最后一次插入所得的id。

解决方案 »

  1.   

    好!我正要用此功能!  pcdreama兄
    mysql_insert_id() 如何使用? 用tablename 作参数吗?
    我试了不行啊!  还是需要php高版本支持??
      

  2.   

    我想错乱应该不至于出现,一般数据库对并发控制得比较好!想问问pcdreamam, 这代码行得通吗?
    <?php
        mysql_connect("localhost", "mysql_user", "mysql_password") or
            die("could not connect");
        mysql_select_db("mydb");    // mysql_query("INSERT INTO mytable (product) values ('kossu')");
        printf ("Last inserted record has id %d\n", mysql_insert_id());
    ?>
      

  3.   

    $sql="INSERT INTO table1(……)VALUES(……)";
    mysql_query($sql);$msgID=mysql_insert_id;return $msgID;
      

  4.   

    纠正上面的一个说法错误,mysql_insert_id()其实本身就是一个查询,只是作成了函数而已。
      

  5.   

    <?php
        mysql_connect("localhost", "mysql_user", "mysql_password") or
            die("could not connect");
        mysql_select_db("mydb");    mysql_query("INSERT INTO mytable (product) values ('kossu')");
        printf ("Last inserted record has id %d\n", mysql_insert_id());
    ?>这样就行了。记住mysql_insert_id()要用在insert后面,这个函数是通过调用mysql的功能,所以不会需要查询。文档:
    int mysql_insert_id ( [resource link_identifier])
    mysql_insert_id() returns the ID generated for an AUTO_INCREMENT column by the previous INSERT query using the given link_identifier. If link_identifier isn't specified, the last opened link is assumed. mysql_insert_id() returns 0 if the previous query does not generate an AUTO_INCREMENT value. If you need to save the value for later, be sure to call mysql_insert_id() immediately after the query that generates the value. Note: The value of the MySQL SQL function LAST_INSERT_ID() always contains the most recently generated AUTO_INCREMENT value, and is not reset between queries. 
      

  6.   

    mysql_insert_id()
    =
    select LAST_INSERT_ID() 
    其实也是查询,只是简化并隐藏了操作罢了