代码之家  ›  专栏  ›  技术社区  ›  Matteo

获取插入后自动生成的ID

  •  2
  • Matteo  · 技术社区  · 14 年前

    我有一个Oracle Express 10g数据库。在我的表中,我有一个自动生成的ID,我想知道如何在插入后找到生成的ID。我目前正在使用PHP。

    1 回复  |  直到 11 年前
        1
  •  7
  •   shamittomar    14 年前

    可以将返回的id放入变量中。例如,此代码:

    $data = array("larry","bill","steve");
    $db = OCILogon("scott","tiger");
    $stmt = OCIParse($db,"insert into names values (myid.nextval,:name) returning id into :id");
    
    OCIBindByName($stmt,":ID",$id,32);
    OCIBindByName($stmt,":NAME",$name,32);
    
    while (list(,$name) = each($data))
    {
         OCIExecute($stmt);
         echo "$name got id:$id\n"; 
    }
    

    这给了你身份证 $name 以变量的形式 $id . 相应地更改SQL。