Link to home
Start Free TrialLog in
Avatar of pjordanna
pjordanna

asked on

get @@identity of last inserted id with out using mssql built in functions

Hi I am trying to get the indentitiy back of the last inserted id, I am having issues installing the mssql extension and need an alternative without using the mssql built in functions. Can any one help? My example code is below. Thanks
<?php
$conn = new COM ("ADODB.Connection") or die("Cannot start ADO");
 
$connStr = "Provider=SQLOLEDB;User ID=****;Password=****;Initial Catalog=****;Data Source=****";
 
$conn->open($connStr);
$query = "SET NOCOUNT ON INSERT INTO 
			carhire_orders(
			BookingID,
			OrderDate) 
		VALUES(
			'$bookingID',
			'$orderDate') SELECT @@IDENTITY AS NewID SET NOCOUNT OFF";
 
$conn->execute($query);
 
// echo last inserted id
 
$conn->Close();
 
$conn = null;
?>

Open in new window

Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

you should use

SELECT SCOPE_IDENTITY() as [NewID]

instead of

SELECT @@IDENTITY AS NewID

(unless you use sql 7...)

anyhow, what problems do you actually run into?
Avatar of pjordanna
pjordanna

ASKER

Hi thanks for you reply, but I sorted it.

$conn->execute($query);
 
echo $rs->Fields(0); // this was the part i was stuck on for some reason...

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial