Skip to content
Advertisement

PHP Return ID[uniqueidentifier] after sql INSERT

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:

And one to insert a new record:

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.

Advertisement

Answer

(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.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement