Contact iFingers

Finger Tips - a fistful of handy web notes

Thursday, July 05, 2007

Get the last record from mysql using php

While developing a new web app, I found a nice little php function that will get the record number of the last inserted record.

The manual states that mysql_insert_id will, "Get the ID generated from the previous INSERT operation", and in my experience, it does exactly what it says on the tin.

mysql_insert_id Examples



$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());

 printf("View Last inserted record id %d\n", mysql_insert_id());

Get the last id



$last_id = mysql_insert_id(); 

Labels: ,