Skip to content
Advertisement

PDO get the last ID inserted

I have a query, and I want to get the last ID inserted. The field ID is the primary key and auto incrementing.

I know that I have to use this statement:

JavaScript

That statement works with a query like this:

JavaScript

But if I want to get the ID using this statement:

JavaScript

I get this error:

JavaScript

What am I doing wrong?

Advertisement

Answer

That’s because that’s an SQL function, not PHP. You can use PDO::lastInsertId().

Like:

JavaScript

If you want to do it with SQL instead of the PDO API, you would do it like a normal select query:

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