2

Possible Duplicate:
SQL: How to get the id of values I just INSERTed?

For PHP, What's equivalent of mysql_insert_id() for sql server 2012?

sqlsrv_insert_id() does not seem to exist.

Say this is the code

  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;
        }

for mysql, how should I do it for sql server?

Community
  • 1
  • 1
william007
  • 15,661
  • 20
  • 90
  • 161

2 Answers2

7

Check this out: http://fr2.php.net/manual/en/book.mssql.php Over there at the comments you have the php syntax at the comments:


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; 
} 

Jonathan Levison
  • 2,617
  • 1
  • 17
  • 22
1

Or query it:

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

TOP is the MySql equivalent of LIMIT

Mike de Klerk
  • 11,202
  • 8
  • 48
  • 73