0

I can get the ID of last inserted records using sql server:

  INSERT INTO CustomerFeedback(CustomerName, Dept, Location, surveydate, workOrderID,yourIPAddress)
  VALUES('Sam Jones','IT','PMO','07/29/2015','10:00:11:0')
  SELECT SCOPE_IDENTITY()

But how do I get the ID of last inserted record using php and sql server?

Every example I have seen so far is for php and MySQL.

Below is the code I am using.

     $strSQL = "INSERT INTO CustomerFeedback(CustomerName, Dept, Location, surveydate, workOrderID,yourIPAddress)
     VALUES ('". ms_escape_string($customername)."','". ms_escape_string($dept)."','". ms_escape_string($location)."','". ms_escape_string($surveydate)."','". ms_escape_string($idworkorder)."','". ms_escape_string($ipaddress)."')";
  $results = sqlsrv_query($con, $strSQL);

Thanks in advance.

Chidi Okeh
  • 1,537
  • 8
  • 28
  • 50
  • 2
    Can you please reference where this question has been asked before using php and sql server? Your example is for sql and c# – Chidi Okeh Jul 29 '15 at 14:33
  • Look again at the duplicate. It doesn't matter what front end you are using. Retrieving this information comes from sql and has nothing to do with the programming language being used. – Sean Lange Jul 29 '15 at 15:07
  • Sorry but I disagree. I have done this with .net. I have done it with classic asp. Programming languages and their semantics are different. I have already shown above that I know how to do it with SQL Server but integrating with a programming language matters because as indicated, they are different. – Chidi Okeh Jul 29 '15 at 15:17
  • I would recommend doing this in a stored proc regardless. That would completely remove the programming language from the equation. If you make a better separation between the layers then the DB dependence goes away. – Sean Lange Jul 29 '15 at 15:25
  • I Figured it out! For anyone interested, here is the solution: ` $strSQL = "INSERT INTO CustomerFeedback(CustomerName, Dept, Location, surveydate, workOrderID,yourIPAddress) VALUES ('". ms_escape_string($customername)."','". ms_escape_string($dept)."','". ms_escape_string($location)."','". ms_escape_string($surveydate)."','". ms_escape_string($idworkorder)."','". ms_escape_string($ipaddress)."'); SELECT SCOPE_IDENTITY()"; $results = sqlsrv_query($con, $strSQL); sqlsrv_next_result($results); sqlsrv_fetch($results); $lastId = sqlsrv_get_field($results, 0);` – Chidi Okeh Jul 29 '15 at 15:39

0 Answers0