The scope of mysql_insert_id() is the MySQL connexion. Not the user + password
, but the actual connection for the current script. (Note that the MySQL connection can also be a parameter of mysql_insert_id)
If you close and re-open the MySQL connexion, mysql_insert_id() will not return the id of the id inserted in the previous one.
If an id has been inserted just after your adding, but by another script execution (I mean with another connexion) the mysql_insert_id() will return your ID, not the ID actually created after your but in another connection.
Example :
$c1 = mysql_connect($srv, $usr, $pwd);
$c2 = mysql_connect($srv, $usr, $pwd);
$sql = "INSERT INTO table1 (col1, col2) VALUES('x', 'y') "
mysql_query($sql, $c1); // first insert
mysql_query($sql, $c2); // second insert
$id1 = mysql_insert_id($c1);
$id2 = mysql_insert_id($c2);
$id1 will be the id inserted first, $id2 the id inserted after.