PHP 手册 
后退  前进 
  
 
 mysql_insert_id
(PHP 3, PHP 4 )mysql_insert_id --  Get the ID generated from the previous INSERT operation 
Description
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. 注: 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. 
警告 
mysql_insert_id() converts the return type of the native MySQL C API function mysql_insert_id() to a type of long (named int in PHP). If your AUTO_INCREMENT column has a column type of BIGINT, the value returned by mysql_insert_id() will be incorrect. Instead, use the internal MySQL SQL function LAST_INSERT_ID() in an SQL query. 
 例子 1. mysql_insert_id Example<?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());
?>
 
 See also: mysql_query().    
后退 起点 前进 
mysql_info 上一级 mysql_list_dbs 
 
 

解决方案 »

  1.   

    16.5 怎样在ODBC中获得一个AUTO_INCREMENT列的值
    一个常见的问题是怎样得到一个自动从一个INSERT产生的ID值,用ODBC,你可以这样做(假定auto是一个AUTO_INCREMENT字段):INSERT INTO foo (auto,text) VALUES(NULL,'text');
    SELECT LAST_INSERT_ID();或,如果你是只是想把ID插入到另外一个表中,你可以这样做:INSERT INTO foo (auto,text) VALUES(NULL,'text');
    INSERT INTO foo2 (id,text) VALUES(LAST_INSERT_ID(),'text');得益于一些ODBC应用程序(至少Delphi和Access),下列查询可被用来找出最新插入的行:SELECT * FROM tbl_name WHERE auto IS NULL;
      

  2.   

    不用这样的方式,我已经写了一个数据库对象$mydb,而不是使用直接的MYSQL连接
    例如:
    <?php $query=".....";
          $mydb->query($query);
          ...
    ?>
     这种情况下,应该怎么办呢,好像上面兄弟的方法也不能保证数据库的完整性。
      

  3.   

    二楼的兄弟,我的系统是架在Linux下的,没有用ODBC(我讨厌微软的东西);自增字段的功能MySql里就有。
      

  4.   

    C语言中
    ----------------------
    20.4.29 mysql_insert_id()
    my_ulonglong mysql_insert_id(MYSQL *mysql) 20.4.29.1 说明
    返回由先前的查询为一个AUTO_INCREMENT列生成的ID。在你执行一个INSERT查询向一个包含AUTO_INCREMENT字段的表中插入后,使用这个函数。注意,如果先前的查询不产生一个AUTO_INCREMENT值,mysql_insert_id()返回0。如果你需要在以后保存该值,必须在查询生成了该值后马上调用mysql_insert_id()。也要注意,SQL的LAST_INSERT_ID()函数总是包含最近生成的AUTO_INCREMENT值,并且在查询之间不被重置,因为该函数的值在服务器端维护。 20.4.29.2 返回值
    有先前的查询更新的AUTO_INCREMENT字段的值。如果在连接上没有先前的询问或如果查询没更新AUTO_INCREMENT值,返回零。20.4.29.3 错误
    无。 ----------------------------
      

  5.   

    php的mysql函数中,有mysql_insert_id这个函数
    $sql="insert into....";
    $res=mysql_query($sql);
    然后
    $new_id=mysql_insert_id();
    就是新增加的纪录的自增id号;
    即使是封装了数据库类,我想,应该仍然可以用这个函数,如果用mysql的话;如果不是很严格,用select max(id) as xxx from
    应该也可以
      

  6.   

    我想结帐,但是现在还没有让我满意的答案。请你仔细看看我的要求,还有回复中的两条补充。
    兄弟,如果php用户手册里有的东西,我还会来问你吗????!!!!
      

  7.   

    子增字段的值可以得到
    至于mysql不支持触发器,只能自己在程序中保证数据同步、一致。
    比如在每张表中增加一个字段,添入生成的唯一id,
    update ... idid=自增id where newid=唯一id...
      

  8.   

    假如说改封装了的数据库连接对象为$mydb,请问GOODNAME;我该怎样使用mysql_insert_id()函数呢???
    你回答了,我们就结贴,真的!!!
      

  9.   

    修改一下数据库封装类,加上一个成员函数,如何?
    数据库封装这事我也干过;
    本来的是,自己的类中,没有这个函数,无法提供这个功能,何必强求???请问你封装了哪几种数据库?如果单单mysql的话,增加一个成员函数很难吗?
      

  10.   

    如果说破坏封装性的话
    执行玩了insert into的sql后
    直接取自增id,一样可以。
    <?php $query=".....";
          $mydb->query($query);
          $mynewid=mysql_insert_id(); //这句,可以执行
         ...
    ?>
      

  11.   

    可以用LOCK将数据库所定,全码查询刚刚插入的那条记录。