ms sql server的等效mysql_insert_id()

时间:2012-10-14 08:34:57

标签: php sql-server sql-server-2012

  

可能重复:
  SQL: How to get the id of values I just INSERTed?

对于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?

2 个答案:

答案 0 :(得分:7)

检查一下: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; 
} 

答案 1 :(得分:1)

或查询它:

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

TOP是MySql等价于LIMIT