How to get the id of an inserted mysql row in php
By liviu - Posted on December 13th, 2008
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());
?> Bookmark/Search this post with:
- liviu's blog
- Login or register to post comments
Delicious
Digg
StumbleUpon
Propeller
Reddit
Magnoliacom
Newsvine
Furl
Facebook
Google
Yahoo
Technorati
Icerocket