I am trying to fetch the last inserted ID using the below query in PHP with the help of sqlsrv_query()
, but it returns me the error:
The active result for the query contains no fields.
Here's my code:
$query = "
insert into saccess(NAME,LOGINID,PASSWORD,USERTYPE,CREATEDON,CREATEDBY)
values('name','user','PASSWORD','USERTYPE',GETDATE(),3);
SELECT SCOPE_IDENTITY() AS LASTID;
";
$result = sqlsrv_query($conn,$query);
echo "Rows affected: ".sqlsrv_rows_affected($result)."<br>";
$next_result = sqlsrv_next_result($result);
echo "NEXT RESULT: ".$next_result."<br>";
if( $next_result ) {
echo "inside next result.<br>";
print_r(sqlsrv_fetch_array( $result, SQLSRV_FETCH_ASSOC));
}
elseif( is_null($next_result)) {
echo "No more results.<br />";
}
print_r(sqlsrv_errors());
The above query works fine in MSSQL, but sqlsrv_query()
seems to be only executing the first.
Here's the output of the code:
Rows affected: 1
NEXT RESULT: 1
inside next result.
Array ( [0] => Array ( [0] => IMSSP [SQLSTATE] => IMSSP [1] => -28 [code] => -28 [2] => The active result for the query contains no fields. [message] => The active result for the query contains no fields. ) )
Please help me out in pointing out what is wrong.
Update:
Definition:
Data:
"; while ($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) {$data[] = $row;}} while (sqlsrv_next_result($result)); echo print_r($data);`. – Zhorov Oct 21 '20 at 06:54