0

Like the title says, I want the id of a row to be returned after INSERT to the database.

I've got 2 functions, one to make a connection to the database:

function db_connect() {
    $host = "host";
    $user = "user";
    $pwd = "pwd";
    $database = "db";
    $con;
    try{
        $conn = new PDO( "sqlsrv:Server= ".$host." ; Database = ".$database." ", $user, $pwd);
    }
    catch(Exception $e){
        die(print_r($e));
    }
    return $conn;
}

And one to insert a new record:

function setTiptile($name,$cols,$rows) {
    $connect = db_connect();
    $query = "INSERT INTO data(ID, name, cols, rows) VALUES(NEWID(),?,?,?)";
    $stmt = $connect->prepare($query);
    $stmt->bindValue(1, $name);
    $stmt->bindValue(2, $cols);
    $stmt->bindValue(3, $rows);
    $stmt->execute();


    return $connect->lastInsertId('ID'); // This should work, but doesn't, why?

}

I need the last function to return the ID of the inserted row, how should I do this?

EDIT: Like the title says, the ID is an uniqueidentifier, no idea if that changes things.

EDIT: Ok, apparently I've got to use:$connect->lastInsertId('ID');, but this isn't returning anything at all. What can be the cause of that? The new row ís created in the database.

2
  • 1
    They're not using MySQL Commented Nov 22, 2013 at 14:06
  • I would like to point out that @JohnConde's comment is inaccurate - this works with SQL Server but only if the column has an identity insert - since uniqueidentifier doesn't have this option, this method won't work. Commented Mar 14, 2024 at 22:46

4 Answers 4

1

From the Manual:

Returns the ID of the last inserted row, or the last value from a sequence object, depending on the underlying driver. For example, PDO_PGSQL() requires you to specify the name of a sequence object for the name parameter.

It should be something like:

return $connect->lastInsertId('yourIdColumn');
Sign up to request clarification or add additional context in comments.

2 Comments

tried this, isn't working! In my case it should be; return $connect->lastInsertId('ID'); right?
Yes Corrected my answer @Jeroen
0

Use lastInsertId

$conn->lastInsertId(); 

Comments

0

(My) solution:

The parameter of lastInsertId() has to be the table name instead of the row name. Besides that, the table must have a row with the parameter IDENTITY checked, this is the row which is returned.

That brings to the conclusion that it's impossible to return a uniqueidentifier with lastInsertId() since this cannot have the parameter IDENTITY checked.

1 Comment

Something else that will work is INSERT INTO <table> [(column1, column2, ... ,columnN)] OUTPUT inserted.id_column VALUES('value1', 'value2', ..., 'valueN')
0

why not first do a select newid() and then use that id in the insert

2 Comments

Can you try to phrase this more as an answer than a question?
Welcome, and thank you @rolf oosting, what you wrote is more likely comment but not answer so rephrasing it in a more structured answer would be more organized.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.