2012-10-14 109 views
1

可能重複:
SQL: How to get the id of values I just INSERTed?等效mysql_insert_id()對MS SQL服務器的

對於PHP,什麼是爲SQL Server 2012等效mysql_insert_id()

sqlsrv_insert_id()似乎並不存在。

說這是代碼

if ($result) { 
      // get user details 
      $uid = mysql_insert_id(); // last inserted id 
      $result = mysql_query("SELECT * FROM users WHERE uid = $uid"); 
      // return user details 
      return mysql_fetch_array($result); 
     } else { 
      return false; 
     } 

爲MySQL, 我應該怎麼做的SQL Server?

+0

嗯..但它不提供一個PHP語法? – william007

+1

PDO有[lastInsertId](http://www.php.net/manual/en/pdo.lastinsertid.php),切換到PDO可能是一個好主意? – fvu

回答

6

檢查了這一點:http://fr2.php.net/manual/en/book.mssql.php 在那邊你有註釋的PHP語法的評論:

 

function mssql_insert_id() { 
    $id = 0; 
    $res = mssql_query("SELECT @@identity AS id"); 
    if ($row = mssql_fetch_array($res, MSSQL_ASSOC)) { 
     $id = $row["id"]; 
    } 
    return $id; 
} 

+0

mysql_fetch_array($ res,MYSQL_ASSOC)..使用mysql for sqlsrv? – william007

+0

不,這沒什麼意義,我從我引用的鏈接複製並粘貼,我認爲他們在那裏有一個錯字,我會修復我複製粘貼的內容,請告訴我們它是否有效:) –

1

或查詢它:

SELECT `column_id` FROM `table` ORDER BY `column_id` DESC TOP 1 

TOP是MySQL當量限值