3

I have a simple table , contains a primary key which has an Auto Increment index.

When I do insertion with mysqli , is there any chance to know the last inserted value of the Auto Increment column ?

Note:

There might be multiple people trying to insert simultaneously

daisy
  • 22,498
  • 29
  • 129
  • 265

5 Answers5

10

You can just use: mysqli_insert_id($con)

Rok Jarc
  • 18,765
  • 9
  • 69
  • 124
bcmcfc
  • 25,966
  • 29
  • 109
  • 181
5

You can use the insert_id value (not a method call) from the database object :)

$id = $db->insert_id;
Antony Carthy
  • 5,549
  • 9
  • 34
  • 38
2

Correction to last post, procedural style takes a parameter "$link", where $link is the identifier returned by mysqli_connect() or mysqli_init():

$last_id = mysqli_insert_id($link);

Mason Barge
  • 409
  • 4
  • 4
1

http://php.net/manual/en/function.mysql-insert-id.php - mysql_insert_id()

verisimilitude
  • 5,077
  • 3
  • 30
  • 35
1
   $last_id = $link->insert_id;

Where $link is MySQLi resource.

Or:

$last_id = mysqli_insert_id();
Vishal
  • 2,161
  • 14
  • 25