Quantcast
Channel: Microsoft Drivers for PHP for SQL Server forum
Viewing all articles
Browse latest Browse all 391

lastinsertid failed with trigger on table

$
0
0

Hi,

i want to get the last insert id after i insert on table content. Table content has a trigger on insert event.

i have the following code that did not work:

$connectionInfo = array( "Database"=>"xxx", "UID"=>"sa", "PWD"=>"xxx");
$conn = sqlsrv_connect( "xxx\SQLEXPRESS", $connectionInfo);

$sql = 'INSERT INTO "content" ("parent", "name", "content", "created_by", "changed_by", "create_date", "change_date") VALUES (?, ?, ?, ?, ?, ?, ?) SELECT SCOPE_IDENTITY() as Current_Identity';
$params = array(1, 'TTTTTTTTTTTTT', 'TTTTTTTTTTTTT', 0, 0, '20121128 20:01:57', '20121128 20:01:57');

$stmt = sqlsrv_query( $conn, $sql, $params);
if( $stmt === false ) {
     die( print_r( sqlsrv_errors(), true));
}

// Make the first (and in this case, only) row of the result set available for reading.
$next_result = sqlsrv_next_result($stmt);

if( sqlsrv_fetch( $stmt ) === false) {
     die( print_r( sqlsrv_errors(), true));
}

// Get the row fields. Field indeces start at 0 and must be retrieved in order.
// Retrieving row fields by name is not supported by sqlsrv_get_field.
$name = sqlsrv_get_field( $stmt, 0);
echo "$name: ";

I need to make two sqlsrv_next_result commands. The following code works:

$connectionInfo = array( "Database"=>"xxx", "UID"=>"sa", "PWD"=>"xxx");
$conn = sqlsrv_connect( "xxx\SQLEXPRESS", $connectionInfo);

$sql = 'INSERT INTO "content" ("parent", "name", "content", "created_by", "changed_by", "create_date", "change_date") VALUES (?, ?, ?, ?, ?, ?, ?) SELECT SCOPE_IDENTITY() as Current_Identity';
$params = array(1, 'TTTTTTTTTTTTT', 'TTTTTTTTTTTTT', 0, 0, '20121128 20:01:57', '20121128 20:01:57');

$stmt = sqlsrv_query( $conn, $sql, $params);
if( $stmt === false ) {
     die( print_r( sqlsrv_errors(), true));
}

// Make the first (and in this case, only) row of the result set available for reading.
$next_result = sqlsrv_next_result($stmt);
$next_result = sqlsrv_next_result($stmt);

if( sqlsrv_fetch( $stmt ) === false) {
     die( print_r( sqlsrv_errors(), true));
}

// Get the row fields. Field indeces start at 0 and must be retrieved in order.
// Retrieving row fields by name is not supported by sqlsrv_get_field.
$name = sqlsrv_get_field( $stmt, 0);
echo "$name: ";

For me it sounds like a bug that i need to make to sqlsrv_next_result.

PHP Version 5.4.7

sqlsrv driver 3.0.1

So what can id do?

regards,

Frank



Viewing all articles
Browse latest Browse all 391

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>