I have this code that will insert some data on my table and i need to get the last inserted ID that is my PK.
$serverName = "MyServer";
$connectionInfo = array( "Database"=>"MyBD", "UID"=>"MyUser", "PWD"=>"MyPass");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn )
{
echo "Connection established.<br />";
}
else
{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
$nome = $_POST["nome"];
$email = $_POST["email"];
$telefone = $_POST["telefone"];
$unidade = $_POST["unidade"];
//declare the SQL statement that will query the database
$query = "INSERT INTO [dbo].[Participante] ([nome],[email],[telefone],[unidadeCE],[dataCadastro]) VALUES ('" . $nome . "', '" . $email . "', '" . $telefone . "', '" . $unidade . "', (getdate())); SELECT @@IDENTITY";
//execute the SQL query and return records
$result = sqlsrv_query($conn, $query);
var_dump($result);
//close the connection
sqlsrv_close($conn);
I have already tried the SELECT @@IDENTITY
but it only returns resource(4) of type (SQL Server Statement)
$query = "INSERT INTO [dbo].[Participante] ([nome],[email],[telefone],[unidadeCE],[dataCadastro]) VALUES ('" . $nome . "', '" . $email . "', '" . $telefone . "', '" . $unidade . "', (getdate())); SELECT SCOPE_IDENTITY()";
$resource=sqlsrv_query($conn, $query, $arrParams);
sqlsrv_next_result($resource);
sqlsrv_fetch($resource);
echo sqlsrv_get_field($resource, 0);
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다