简体   繁体   English

PHP使用Scope_identity返回ID

[英]php to return id using Scope_identity

Searching other articles I have come to the conclusion I need to use Scope_Identity() to return the value of the primary key that gets generated when I insert a row into my MS SQL table. 在搜索其他文章时,我得出的结论是,我需要使用Scope_Identity()返回将行插入MS SQL表时生成的主键的值。 However what is not so clear for me is exactly the php I need to return that value. 但是,对我来说还不清楚的是我需要返回该值的php。 Being fairly new, I have gotten away until now by borrowing code from my predecessor to run sql queries, but they have always been situations where I am returning results from an Array using a while loop. 作为一个相当新的人,直到现在我还是从以前的工作中借用了代码来运行sql查询,从而摆脱了困境,但是在这些情况中,我总是使用while循环从Array返回结果。 Please advise the correct code. 请告知正确的代码。

$query = "INSERT INTO mytable (Title,Author)
          VALUES('".sql_safe($Title)."', '"sql_safe($Author)."'); SELECT Scope_Identity() as myid;";

$result = odbc_exec($con1, $query);

until now I have only ever needed to use 到现在为止,我只需要使用

while($row = odbc_fetch_array($result)){ }

but what is the correct way of referring to that myid ? 但是提到那个myid的正确方法是什么?

I struggled with this for a while and in the end my solution involved going back to mssql_query driver 我为此了一段时间,最后我的解决方案涉及回到mssql_query驱动程序

$query ="        INSERT INTO powerview.DL_documentlist (Title,Liveversion,Author,Location,Locked,LockedbyID)
                VALUES('".sql_safe($Title)."',1.0,'".sql_safe($Author)."','".sql_safe($Location)."','1',".$lockedbyID."); 
                SELECT SCOPE_IDENTITY() as id;
                RETURN;
             ";

    $result = mssql_query($query);      
    $result = mssql_fetch_object($result);

    $DocID = $result->id;  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM