Henkka
Henkka

Reputation: 1663

How to get the last inserted ID?

I am trying to fetch the last inserted ID from MySQL database in PHP and I was wondering what would be the best way to do it?

I am currently using this kind of solution, but there has to be a better and safer way. So would someone please help me? Thanks

Some insert statement...

 $query = "SELECT MAX(aid) FROM asset";
 $result = mysql_query($query);
                            while($row = mysql_fetch_array($result, MYSQL_ASSOC))
                                    {
                                    $pickassssult= $row['MAX(aid)'];


                                    }

Upvotes: 0

Views: 227

Answers (5)

J0HN
J0HN

Reputation: 26951

Use mysql_insert_id(). Hovewer, it's not absolutely safe. Here is a manual

Upvotes: 0

ChrisG
ChrisG

Reputation: 314

You can get the insert id by calling the mysql_insert_id() function. See mysql_insert_id()

Upvotes: 0

silverstrike
silverstrike

Reputation: 522

MySQL command:last_insert_id()

Upvotes: 0

Bart Blommaerts
Bart Blommaerts

Reputation: 821

See the manual: mysql_insert_id

Upvotes: 0

blejzz
blejzz

Reputation: 3349

PHP has a function called:

 mysql_insert_id();

more info http://lt.php.net/manual/en/function.mysql-insert-id.php

Upvotes: 5

Related Questions