How to get the id of an inserted mysql row in php

Tagged:  

This saved me a lot of work after i found out about it. After you insert a row in a mysql table, you can get the id that was automatically created using this php funtion: mysql_insert_id ([ resource $link_identifier ] ).

mysql_insert_id — Get the ID generated from the previous INSERT operation

int mysql_insert_id ([ resource $link_identifier ] )

Basically it's like $last_inserted_id = mysql_insert_id($conection);

The example from php.net:

<?php
$link 
mysql_connect('localhost''mysql_user''mysql_password');
if (!
$link) {
    die(
'Could not connect: ' mysql_error());
}
mysql_select_db('mydb');

mysql_query("INSERT INTO mytable (product) values ('kossu')");
printf("Last inserted record has id %d\n"mysql_insert_id());
?>