Last inserted value on SQL Server with PHP

Ariel

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)

Pupil
$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); 

Reference:

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

how to get last inserted record by username in sql server

분류에서Dev

Executing Sql Server Stored Procedure and getting OUTPUT INSERTED value

분류에서Dev

how to get last inserted value when multiple users enter details in form doing in php and oracle

분류에서Dev

SQL Server trigger and INSERTED and DELETED table records

분류에서Dev

SQL Server-INSERTED.OUTPUT-변수 할당

분류에서Dev

SQL Server Merge - Output returning null deleted rows as inserted

분류에서Dev

SQL Server : duplicates value

분류에서Dev

last_inserted_id 가져 오기-PHP-Wordpress

분류에서Dev

how to print the last record of SQL server

분류에서Dev

Pass textarea value to server php

분류에서Dev

SQL Server 트리거-INSERTED의 각 행을 사용하는 방법

분류에서Dev

SQL Server 트리거 : INSERTED 및 DELETED가 없습니다. 뭐야?

분류에서Dev

how to get last inserted primary key

분류에서Dev

LAST_VALUE ()가 SQL Server에서 작동하지 않는 이유는 무엇입니까?

분류에서Dev

SQL/PHP - read part of value

분류에서Dev

SQL Server LAST와 유사한 기능

분류에서Dev

SQL Server : the last statement included within a function must be a return statement

분류에서Dev

php submit only giving last value from array that populates form

분류에서Dev

How to insert & update the same inserted multiple record in SQL Server 2008 R2 using multithread in windows service(C#)?

분류에서Dev

SQL Server 2008 R2에서 "INSERTED"기록 바인딩이 지연됩니까?

분류에서Dev

Remove Last Word in SQL row if maxlength is reached? (PHP)

분류에서Dev

SQL server , Add data in multi value attribute

분류에서Dev

Varchar value not passing into SQL Server stored procedure

분류에서Dev

SQL SERVER STUFF FUNCTION VALUE INCREMENT COUNTER

분류에서Dev

MS SQL Server에 PHP 연결

분류에서Dev

Sqlite query in Android to get the last inserted row by other id

분류에서Dev

Get last inserted row's complex primary key

분류에서Dev

SQL (MS SQL Server) return column value as the result of the multiplication

분류에서Dev

SQL Server - Select column to update based on another column value

Related 관련 기사

  1. 1

    how to get last inserted record by username in sql server

  2. 2

    Executing Sql Server Stored Procedure and getting OUTPUT INSERTED value

  3. 3

    how to get last inserted value when multiple users enter details in form doing in php and oracle

  4. 4

    SQL Server trigger and INSERTED and DELETED table records

  5. 5

    SQL Server-INSERTED.OUTPUT-변수 할당

  6. 6

    SQL Server Merge - Output returning null deleted rows as inserted

  7. 7

    SQL Server : duplicates value

  8. 8

    last_inserted_id 가져 오기-PHP-Wordpress

  9. 9

    how to print the last record of SQL server

  10. 10

    Pass textarea value to server php

  11. 11

    SQL Server 트리거-INSERTED의 각 행을 사용하는 방법

  12. 12

    SQL Server 트리거 : INSERTED 및 DELETED가 없습니다. 뭐야?

  13. 13

    how to get last inserted primary key

  14. 14

    LAST_VALUE ()가 SQL Server에서 작동하지 않는 이유는 무엇입니까?

  15. 15

    SQL/PHP - read part of value

  16. 16

    SQL Server LAST와 유사한 기능

  17. 17

    SQL Server : the last statement included within a function must be a return statement

  18. 18

    php submit only giving last value from array that populates form

  19. 19

    How to insert & update the same inserted multiple record in SQL Server 2008 R2 using multithread in windows service(C#)?

  20. 20

    SQL Server 2008 R2에서 "INSERTED"기록 바인딩이 지연됩니까?

  21. 21

    Remove Last Word in SQL row if maxlength is reached? (PHP)

  22. 22

    SQL server , Add data in multi value attribute

  23. 23

    Varchar value not passing into SQL Server stored procedure

  24. 24

    SQL SERVER STUFF FUNCTION VALUE INCREMENT COUNTER

  25. 25

    MS SQL Server에 PHP 연결

  26. 26

    Sqlite query in Android to get the last inserted row by other id

  27. 27

    Get last inserted row's complex primary key

  28. 28

    SQL (MS SQL Server) return column value as the result of the multiplication

  29. 29

    SQL Server - Select column to update based on another column value

뜨겁다태그

보관