<?php
//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.    mysql_connect("localhost", "mysql_user", "mysql_password") or
        die("Could not connect: " . mysql_error());
    mysql_select_db("mydb");    mysql_query("INSERT INTO mytable (product) values ('kossu')");
    printf ("Last inserted record has id %d\n", mysql_insert_id());
?>

解决方案 »

  1.   

    你插入一条数据以后,接着mysql_insert_id();
    就是这条数据的ID
      

  2.   

    我是觉得有机可乘,比如刚插入一条记录,还没运行到mysql_insert_id();这句的时候另外又插入一条,这时候才运行到mysql_insert_id(),这样不是就出错了吗?会不会出现这样的错误呢?是几率很小,还是不会呢?
      

  3.   

    不会!mysql_insert_id()返回的是当前连接的最后插入记录的id。
    如果这一点都不能保证,那也不能叫他数据库了。呵呵。