Reputation: 1663
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
Reputation: 26951
Use mysql_insert_id()
. Hovewer, it's not absolutely safe. Here is a manual
Upvotes: 0
Reputation: 314
You can get the insert id by calling the mysql_insert_id() function. See mysql_insert_id()
Upvotes: 0
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