title: Retrieve the ID of an inserted record: Php & MS SQL SERVER
body: <blockquote>
  <p>This question relates to:</p>
  <p>PHP Version 5.3.6</p>
  <p><a href="http://msdn.microsoft.com/en-us/library/cc296170(v=sql.90).aspx" rel="noreferrer">Microsoft Drivers for PHP for SQL Server</a></p>
  </blockquote>
  <p>I am trying to properly retrieve the ID of a record insert using a combination of PHP and SQL Server 2005 or 2008.</p>
  <p>This question assumes the existence of the following table:</p>
  <p><code>CREATE TABLE users([id] [int] IDENTITY(1,2) NOT NULL,[username] [varchar](50) NOT NULL,[password] [varchar](40) NOT NULL,[first_name] [varchar](30) NOT NULL,[last_name] [varchar](30) NOT NULL)</code></p>
  <p>If I were to run the following query in Management Studio or via ASP (Classic), a record would be inserted and an ID would be returned, but the same behavior does not seem to exist with PHP and this driver.</p>
  <p><code>INSERT INTO users  (username, password, first_name, last_name) VALUES ('x','x','x','x')  ; SELECT @@IDENTITY ;</code></p>
  <p>I need help figuring out how to properly pull an ID either by chaining SQL statements together or by any other method.</p>
  <p>I have worked up some code.</p>
  <p>Variation 1 is a simple select (via query) - no insertion or ID retrieveal performed</p>
  <p>Variation 2 is a chained SQL statement which inserts the new record properly, but does not return the ID</p>
  <p>Variation 3 uses (execute) instead of (query). The record is inserted, but the ID is not returned. This one generated an error because sqlsrv_execute returns true/false instead of a recordset.</p>
  <p>So how can I modify my code to insert a record and retrive an ID? (I'm working on the assumption that the answer to this question will extend to Stored Procedures that return data as well, but perhaps that is not the case)</p>
  <p>Thank you.</p>
  <pre><code>// Database connection
  // -------------------------------------------------------------------------------------------------------
  $conn = sqlsrv_connect(DB_SERVER,array(&quot;UID&quot; =&gt; DB_USER, &quot;PWD&quot; =&gt; DB_PASSWORD, &quot;Database&quot;=&gt; DB_NAME ));
  
  // Variation 1, straight select
  // -------------------------------------------------------------------------------------------------------
  $sql = &quot;SELECT * FROM users;&quot;;
  $result = sqlsrv_query($conn,$sql);
  $row = sqlsrv_fetch_array($result);
  
  // Variation 2, Insert new record and select @@IDENTITY
  // -------------------------------------------------------------------------------------------------------
  $sql = &quot;INSERT INTO users  (username, password, first_name, last_name) VALUES ('x','x','x','x')  ; SELECT @@IDENTITY ;&quot;;
  $result = sqlsrv_query($conn,$sql);
  $row = sqlsrv_fetch_array($result);
  
  // Variation 3, using EXECUTE instead of QUERY
  // -------------------------------------------------------------------------------------------------------
  //$sql = &quot;INSERT INTO users  (username, password, first_name, last_name) VALUES ('x','x','x','x')  ; SELECT @@IDENTITY as id ;&quot;;
  $sql = &quot;INSERT INTO users  (username, password, first_name, last_name) VALUES ('x','x','x','x')  ; SELECT SCOPE_IDENTITY() as id ;&quot;;
  
  $stmt = sqlsrv_prepare( $conn, $sql);
  $result = sqlsrv_execute($stmt);
  $row = sqlsrv_fetch_array($result);
  </code></pre>